'Is this correct for rock-paper-scissor game | Python?
The question is : Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
player1 = input("Player 1: ")
player2 = input("Player 2: ")
if player1 == "rock" and player2 == "paper":
print("Player 2 is the winner!!")
elif player1 == "rock" and player2 == "scissor":
print("Player 1 is the winner!!")
elif player1 == "paper" and player2 == "scissor":
print("Player 2 is the winner!!")
elif player1 == player2:
print("It's a tie!!")
After asking if the players want to start a new game, how to restart the code?
Solution 1:[1]
You could use a while loop, i.e. something like
play = 'Y'
while play=='Y':
# your original code
play = input("Do you want to play again (Y/N): ")
Also I'd suggest you check that the responses from the user are valid (i.e. what if they type potatoe, in your code nothing will print if one or both players don't respond with rock, scissors or paper. You should also look at the lower() command and convert the answer to lower case for this reason.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Waterworth |
