-2

I'm a beginner and writing a code for "Rock Paper Scissors" game. I don't want to run this game(code) over and over again, hence, used while loop. Now, at the "else:" step when any invalid choice is typed by the Player, Computer plays it's turn too after which "Invalid choice. Play your turn again!" is displayed.

I want when a Player types any invalid choice, the computer should not play it's turn and we get "Invalid choice. Play your turn again!" displayed, keeping the game running.

Please check my code and point me to the issue. Please explain with the correction. Thanks in advance!

print("Welcome to the famous Rock Paper Scissors Game. \n")

Choices = ["Rock", "Paper", "Scissors"]

while(True):
    Player = input("Your turn: ")
    Computer = random.choice(Choices)
    print(f"Computer's turn: {Computer} \n")
        
    if Player == Computer:
        print("That's a tie, try again! \n")
    elif Player == "Rock" and Computer == "Scissors":
        print("You Won!!! \n")
    elif Player == "Rock" and Computer == "Paper":
        print("Computer won! \n")
    elif Player == "Paper" and Computer == "Scissors":
        print("Computer won! \n")
    elif Player == "Paper" and Computer == "Rock":
        print("You Won!!! \n")
    elif Player == "Scissors" and Computer == "Paper":
        print("You Won!!! \n")
    elif Player == "Scissors" and Computer == "Rock":
        print("Computer won! \n")
    else:
        print("Invalid choice. Play your turn again! \n")
  • 2
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Brian61354270 Nov 19 '21 at 03:57
  • 1
    Try defining a function that gets and validates user input. Breaking problems down into smaller problem is key to success in programming. – Chris Nov 19 '21 at 04:02
  • Just define a function for asking the user for the input and make a while loop inside it as while the input isn't valid print not valid and ask for the input again....and of course call the function once out of it and call it inside itself -I don't remember the name of this kind of functions- to rerun the function if the input is invalid – Mohamed Yahya Nov 19 '21 at 04:06
  • Thanks, but honestly, I haven't gone that far(functions, exceptions). I know going with the further lectures I can implement them to get a great game(the way I like) but was wondering if that could be achieved without using functions/exceptions. – Ashish Gupta Nov 19 '21 at 04:08
  • 1
    @MohamedYahya, it is called recursive function or [recursion](https://www.programiz.com/python-programming/recursion) – PCM Nov 19 '21 at 04:13

1 Answers1

1

You can check if the input is valid before the computer plays, and ask again if it is invalid using continue -

Choices = ["Rock", "Paper", "Scissors"]

while(True):
    Player = input("Your turn: ")
    if Player not in Choices: # If it is not in Choices list above
        print("Invalid choice. Play your turn again! \n")
        continue # This will re run the while loop again.
    
    # If Player gives valid input, then continues this

    Computer = random.choice(Choices)
    print(f"Computer's turn: {Computer} \n")

    # The next lines ....

Also check out - Break, Continue and Pass

PCM
  • 2,881
  • 2
  • 8
  • 30