Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissors, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws in python
Code
import random
print("Welcome to the Rock, Paper, Scissors game!")
print("Enter 0 for scissors, 1 for rock, 2 for paper")
while True:
user = int(input("Enter your choice: "))
computer = random.randint(0, 2)
if user > 2 or user < 0:
print("Invalid choice!")
continue
if user == computer:
print("Tie!")
elif user == 0 and computer == 1:
print("You lose!")
elif user == 1 and computer == 2:
print("You lose!")
elif user == 2 and computer == 0:
print("You lose!")
else:
print("You win!")
if input("Do you want to play again? (y/n): ") == "n":
break
Output
0 Comments
Post a Comment