viligen

memory_game

Oct 20th, 2021
1,005
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def is_valid(index_1, index_2, any_list):
  2.     if 0 > index_1 or index_1 >= len(any_list) or 0 > index_2 or index_2 >= len(any_list):
  3.         return False
  4.     return True
  5.  
  6.  
  7. elements = input().split()
  8.  
  9. game_over = False
  10. counter = 0
  11. while True:
  12.     element = input().split()
  13.     if "end" in element:
  14.         break
  15.     counter += 1
  16.     index1 = int(element[0])
  17.     index2 = int(element[1])
  18.     if index1 == index2 or not is_valid(index1, index2, elements):
  19.         middle = len(elements)//2
  20.         new_element = f"-{counter}a"
  21.         elements.insert(middle, new_element)
  22.         elements.insert(middle, new_element)
  23.         print("Invalid input! Adding additional elements to the board")
  24.     elif elements[index1] == elements[index2]:
  25.         if index1 > index2:
  26.             popped = elements.pop(index1)
  27.             elements.pop(index2)
  28.         else:
  29.             popped = elements.pop(index2)
  30.             elements.pop(index1)
  31.         print(f"Congrats! You have found matching elements - {popped}!")
  32.         if len(elements) == 0:
  33.             print(f"You have won in {counter} turns!")
  34.             game_over = True
  35.             break
  36.     else:
  37.         print("Try again!")
  38.  
  39. if not game_over:
  40.     print(f"Sorry you lose :(")
  41.     print(*elements)
  42.  
  43.  
RAW Paste Data