-1
#This program plays rock, paper, scissors between 2 players 

print("Let's play Rock, Paper, Scissors.")
print(" ")
player1 = input("Player 1, Enter your choice: ")
player2 = input("Player 2, Enter your choice: ")


# use if/else statements with nested structures to play
# rock < paper  and rock > scissors
# paper < scissors and paper > rock
# scissors < rock and scissors > paper

while True:
    if (player1.casefold() == "r" and player2.casefold() == "p"):
        print("Rock v. Paper")
        print("Player 2 wins")
    elif (player1.casefold() == "p" and player2.casefold() == "r"):
        print("Rock v. Paper")
        print("Player 1 wins")
    elif (player1.casefold() == "p" and player2.casefold() == "p" or player1.casefold() == "r" and player2.casefold() == "r"):
        print("It's a TIE")
    else:     
        print("[ERROR: m not a valid move.]")
        
        #How to implement a while loop to iterate the game as long as players say yes to playing again
        print(" ")
        playAgain = input("again?\n")
        if (playAgain.casefold() != "y"):
            print("Nice game!")
        break

I am still working on it so right now it's only rock and paper, but I'm not too sure why the while loop won't break. It actually never gets around to asking for user input as to whether they would like to play again.

  • Remember: code never "isn't working". Unless you get a compiler error, the code is doing exactly what you told it to do, and saying that it doesn't work doesn't tell people anything. Instead, explain what you tried to write, show the code you wrote, and talk about what you're seeing it do and (based on the clues that gave you) what you already did in terms of debugging. Having said that, you're almost guaranteed missing parentheses in that last statement, because you're mixing `and` and `or`. – Mike 'Pomax' Kamermans Mar 03 '23 at 00:43
  • Please think carefully about the indentation of the code. Should `if (playAgain.casefold() != "y"):` happen regardless of the user input, for that round, or should it only happen when the input is invalid? Therefore, should that code be indented inside the `else` block, or not? Is it? Similarly, think about when the `break` should happen - every time, or only when `playAgain.casefold() != "y"`? Finally, think about when the `player1` and `player2` values should be chosen - only once at the beginning of the program, or every time the game is played? – Karl Knechtel Mar 03 '23 at 00:44

1 Answers1

-1

I believe the break statement is placed outside of the if statement that checks if the user wants to play again. This means that the loop will always break after the first iteration, regardless of whether the player wants to continue playing or not.

print(" ")
playAgain = input("Do you want to play again? (y/n)\n")
    if (playAgain.casefold() != "y"):
        print("Nice game!")
        break
CRM000
  • 35
  • 10