Python Forum
Rock, paper, scissors spelling error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock, paper, scissors spelling error
#3
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.
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': break
That 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': break
You 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 1
As 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.
banidjamali likes this post
Reply


Messages In This Thread
RE: Rock, paper, scissors spelling error - by buran - Jan-19-2021, 08:52 AM
RE: Rock, paper, scissors spelling error - by deanhystad - Jan-19-2021, 11:38 AM
RE: Rock, paper, scissors spelling error - by buran - Jan-19-2021, 01:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I attempted to make a rock paper scissors bot for homework Intellectual11 3 3,026 Jun-24-2021, 08:00 PM
Last Post: deanhystad
  Rock, Paper, Scissors Game kramon19 2 5,497 Jan-10-2020, 08:18 AM
Last Post: perfringo
  I need help with a python Rock Paper Scissors game urgently. Riff_Raff 3 6,037 Dec-05-2018, 09:13 PM
Last Post: nilamo
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,325 Jul-21-2018, 07:32 PM
Last Post: EvanCahill
  Rock Paper Scissors Warmlawpk441 4 5,286 Oct-11-2017, 10:55 AM
Last Post: gruntfutuk
  Rock paper scissors game samiraheen 3 6,571 Oct-03-2017, 07:07 PM
Last Post: buran
  The Python Book altered rock paper scissors Python_Noob 0 3,029 Sep-18-2017, 06:13 AM
Last Post: Python_Noob
  HELP---problems with rock paper scissors games kalt91 2 4,279 Sep-15-2017, 04:51 PM
Last Post: micseydel
  Rock, Paper, Scissors game help.. hentera 3 5,198 May-19-2017, 10:56 PM
Last Post: ichabod801
  Rock Paper Scissors game codeobri 3 13,585 Apr-28-2017, 01:02 AM
Last Post: codeobri

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020