0

I am a newer programmer. I am trying to build the infamous rock-paper-scissors game. I am working on making infinite loops, break statements and making the game print out the winner and prompt the user to play again. However, I am having trouble with prompting the user to play again. No matter the answer the user inputs, the program will start again. I am not sure what I'm doing wrong.

Here is the code:

#defining the game function
def game():
   
    while True:
        p1=input("Player 1,Rock, Paper, or Scissors?: \n")
        p2= input("Player 2, what's your play?: \n")

        if p1 == "rock":
            if p2 == "scissors":
                print ("player 1 wins")
            elif p2 == "paper":
                print ("player 2 wins")


        elif p1== "paper":
            if p2 == "rock":
                print ("player 2 wins")
            elif p2 == "scissors":
                print ("player 1 wins")

        elif p1 == "scissors":
            if p2 =="rock":
                print ("player 2 wins")
            elif p2 == "paper":
                print ("player 1 wins") 

        else:
                p1 == p2
                print ("its a tie")
#ask the user if they'd like to play again
#the issue is in this section of code
#no mater what the input is, the program will always prompt and start the function again

        ask= input("do you want to play again?: \n")
        if ask == 'y' or 'yes':
                game()
        else:
                ask == 'n' or 'no'
                print ("game over")
                break
      
                

#run program            
game()

#output
#Player 1,Rock, Paper, or Scissors?: 
#rock
#Player 2, what's your play?: 
#paper
#player 2 wins
#do you want to play again?: 
#n
#Player 1,Rock, Paper, or Scissors?: 

I've tried reversing the y and n prompts but this just reverses the problem (makes the game end regardless of the input). I've tried to change the formatting as well, but that always results in an error. Any help would be greatly appreciated.

Stanley
  • 1
  • 1
  • 2
    You also don't need to make a recursive call to `game`; you are already in an infinite loop that will continue as long as you don't explicitly execute a `break` statement. – chepner Dec 16 '22 at 21:48
  • in the "yes" case, change the function call to "continue". And in the "no" case, change the break to "return" – Davi A. Sampaio Dec 16 '22 at 21:49
  • @chepner Change the "don't need to" with "should not" and I agree. ;) – Matthias Dec 16 '22 at 21:50
  • @Matthias That's how I would have phrased it if the loop wasn't already in place. – chepner Dec 16 '22 at 21:51
  • (Recursion is a reasonable way to implement an infinite loop, *if* your language does tail-call optimization. Python is not one of those languages.) – chepner Dec 16 '22 at 21:52
  • @Stanley you'll want to review the [Operator Precedence](https://docs.python.org/3/reference/expressions.html?highlight=evaluation%20order#operator-precedence) section of the Python docs to help you understand why the test `if ask == 'y' or 'yes':` isn't quite right. The [accepted answer](https://stackoverflow.com/a/15112149/3534) of the linked duplicate question explains things in good detail. – Robert Groves Dec 16 '22 at 21:59
  • Hello all, thank you so much. I changed if ask == 'y' or ask == 'yes' and that worked great. I will play with the other suggestions as well. Happy programing. – Stanley Dec 16 '22 at 22:21

0 Answers0