Jan-19-2021, 11:38 AM
(This post was last modified: Jan-19-2021, 01:02 PM by deanhystad.)
The way your game is written, each combination has two possible outcomes when there should be three; player 1 wins, player 2 wins, player 2 had a typo.
If player 1 == player 2, tie.
If player 2 == (player 1 + 1) % 3, player 1 wins, else player 2 wins.
while True: choice1 = input(f"{player1}, start the game:(rock/paper/scissors) ") choice2 = input(f"{player2}, it's your turn:(rock/paper/scissors) ") if choice1 == choice2: print("Same!") elif choice1 == 'rock': if choice2 == 'paper': print(f"{player2} wins!") elif choice2 == 'scissors': print(f"{player1} wins!") else: print("Check the spelling.") elif choice1 == 'paper': if choice2 == 'scissors': print(f"{player2} wins!") elif choice2 == 'rock': print(f"{player1} wins!") else: print("Check the spelling.") elif choice1 == 'scissors': if choice2 == 'rock': print(f"{player2} wins!") elif choic2 == 'paper': print(f"{player1} wins!") else: print("Check the spelling.") else: print("Check the spelling.") if input("Continue?(y/n) ") == 'n': breakThat makes things really unwieldly. As buran suggests, the game logic is improved if you verify the input before playing the game.
player1 = input("Give player 1 a name:\n") player2 = input("Give player 2 a name:\n") while True: choice1 = verified_input(player1) choice2 = verified_input(player2) if choice1 == choice2: print("Same!") elif choice1 == 'rock': if choice2 == 'paper': print(f"{player2} wins!") else: print(f"{player1} wins!") elif choice1 == 'paper': if choice2 == 'scissors': print(f"{player2} wins!") else: print(f"{player1} wins!") elif choice1 == 'scissors': if choice2 == 'rock': print(f"{player2} wins!") else: print(f"{player1} wins!") if input("Continue?(y/n) ") == 'n': breakYou can use the modulo operator to decide the winner. If we map the choices to integers, rock/paper/scissors = 0/1/2, the logic of the game becomes:
If player 1 == player 2, tie.
If player 2 == (player 1 + 1) % 3, player 1 wins, else player 2 wins.
def play(players): """Play a round. Returns 0,1:players[N] wins, 2: Tie""" choices = {'rock':0, 'paper':1, 'scissor':2} p0 = verified_input(choices, players[0]) p1 = verified_input(choices, players[1]) if p0 == p1: return 2 elif p1 == (p0 + 1) % 3: return 0 return 1As buran also suggests, this logic can also be embedded in a dictionary;
winner = { 'rock/rock': 2, 'rock/paper': 1, 'rock/scissor': 0, 'paper/paper': 2, 'paper/scissor': 1, 'paper/rock': 0, 'scissor/scissor': 2, 'scissor/rock': 1, 'scissor/paper': 0 }Notice the repeating pattern of 2, 1, 0? That is what the modulo operator does in the prior example.