Advertisement
Spocoman

03. Memory Game

Nov 6th, 2023
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. game = input().split(' ')
  2. command = input()
  3. counter = 0
  4.  
  5. while command != "end":
  6.     indexes = command.split(' ')
  7.     indexes.sort()
  8.     index1 = int(indexes[0])
  9.     index2 = int(indexes[1])
  10.     counter += 1
  11.  
  12.     if 0 <= index1 < len(game) and 0 <= index2 < len(game) and index1 != index2:
  13.         if game[index1] == game[index2]:
  14.             print(f"Congrats! You have found matching elements - {game[index1]}!")
  15.             game.pop(index2)
  16.             game.pop(index1)
  17.             if len(game) == 0:
  18.                 print(f"You have won in {counter} turns!")
  19.                 exit(0)
  20.         else:
  21.             print("Try again!")
  22.     else:
  23.         print("Invalid input! Adding additional elements to the board")
  24.         game.insert(len(game) // 2, f"-{str(counter)}a")
  25.         game.insert(len(game) // 2, f"-{str(counter)}a")
  26.  
  27.     command = input()
  28.  
  29. print(f"Sorry you lose :(\n{' '.join(game)}")
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement