Advertisement
differen71

memory_game

Oct 21st, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. def try_match(index1, index2, list_of_elements):
  2.     first_element = ''
  3.     second_element = ''
  4.     for index, element in enumerate(list_of_elements):
  5.         if index == index1:
  6.             first_element = element
  7.         elif index == index2:
  8.             second_element = element
  9.     if first_element == second_element:
  10.         list_of_elements.remove(first_element)
  11.         list_of_elements.remove(second_element)
  12.         print(f"Congrats! You have found matching elements - {first_element}!")
  13.     else:
  14.         print(f"Try again!")
  15.  
  16.  
  17. def invalid_index(moves, list_of_elements):
  18.     first_half_of_list = len(list_of_elements) // 2
  19.     for index, element in enumerate(list_of_elements):
  20.         if index == first_half_of_list:
  21.             list_of_elements.insert(index, f"-{moves}a")
  22.             list_of_elements.insert(index + 1, f"-{moves}a")
  23.             print(f"Invalid input! Adding additional elements to the board")
  24.  
  25.  
  26. def game_progress(list_of_elements):
  27.     moves_counter = 0
  28.     player_won = False
  29.     while True:
  30.         command = input()
  31.         if command == 'end':
  32.             break
  33.         moves_counter += 1
  34.         first_index, second_index = map(int, command.split())
  35.  
  36.         if first_index < 0 or first_index > len(sequence_of_elements) or second_index < 0 or second_index > len(
  37.                 sequence_of_elements) or first_index == second_index:
  38.             invalid_index(moves_counter, sequence_of_elements)
  39.  
  40.         else:
  41.             try_match(first_index, second_index, sequence_of_elements)
  42.  
  43.         if len(sequence_of_elements) == 0:
  44.             player_won = True
  45.             break
  46.     if player_won:
  47.         print(f"You have won in {moves_counter} turns!")
  48.     else:
  49.         print(f"Sorry you lose :(\n{' '.join(list_of_elements)}")
  50.  
  51.  
  52. sequence_of_elements = input().split()
  53. game_progress(sequence_of_elements)
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement