0

I am making a rock paper scissors game. Everything works, except I cannot understand why the second while loop is not working in this code. I want to make the program so that if the user does not enter "R" or "P" or "S" then the user will be told it was an invalid entry and it will prompt the user to enter their answer again. It works perfectly fine for player1, but it does not work for player2. For player2, if you do not enter either "R" or "P or "S", then it will prompt you to enter a new value again, but only once, regardless of what you put in. All help appreciated!

if playGame == "Y":
    print("Ok, here we go.")

    player1 = input("Player1, what is your choice, R, P, or S? ")
    player1 = player1.upper()

    while player1 != 'R' and player1 != 'P' and player1 != 'S':
            player1 = input("Invalid answer.  Please answer R, P, or S: ")
            player1 = player1.upper()

    player2 = input("Player2, what is your choice, R, P, or S? ")
    player2 = player2.upper()

    while player2 != 'R' and player2 != 'P' and player2 != 'S':
            player2 = input("Invalid answer.  Please answer R, P, or S: ")
            player2 = player1.upper()
1
  • 1
    The last line of your code should be the following: player2 = player2.upper Commented Feb 8, 2015 at 2:32

2 Answers 2

5

The error is in the last line

player2 = player1.upper()

Should be

player2 = player2.upper()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I completely missed that while looking through the code. I made the change, and the program works now. Thanks again.
I would also suggest removing the inequality checks and using not in instead.
0

Used while loop to ask user again and again when user entered invalid value.

code:

playGame = raw_input("You want to play game: if yes then enter Y:").upper()
if playGame == "Y":
    print("Ok, here we go.")
    player1 = ''
    while  1:
        player1 = raw_input("Player1, what is your choice, R, P, or S?:").upper()
        if player1 not in ['R', 'P', 'S']:
            print "Invalid answer."
        else:
            break

    player2 = ''
    while 1:
        player2 = raw_input("Player2, what is your choice, R, P, or S?:").upper()       
        if player2 not in ['R', 'P', 'S']:
            print "Invalid answer."
        else:
            break

    print "player1:", player1
    print "player2:", player2

output:

vivek@vivek:~/Desktop/stackoverflow$ python 7.py
You want to paly game: if yes then enter Y:Y
Ok, here we go.
Player1, what is your choice, R, P, or S?:a
Invalid answer.
Player1, what is your choice, R, P, or S?:R
Player2, what is your choice, R, P, or S?:w
Invalid answer.
Player2, what is your choice, R, P, or S?:q
Invalid answer.
Player2, what is your choice, R, P, or S?:s
player1: R
player2: S

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.