22A simple, interactive Rock, Paper, Scissors game.
33The user plays against the computer, which makes a random choice.
44"""
5+
56import random
67
8+
79def play_rock_paper_scissors ():
810 """
911 Starts an interactive session of Rock, Paper, Scissors.
1012 This function is interactive and does not have a testable return value.
1113 """
1214 options = ["rock" , "paper" , "scissors" ]
13-
15+
1416 while True :
15- user_choice = input ("Choose rock, paper, or scissors (or 'quit' to exit): " ).lower ()
16-
17+ user_choice = input (
18+ "Choose rock, paper, or scissors (or 'quit' to exit): "
19+ ).lower ()
20+
1721 if user_choice == "quit" :
1822 print ("Thanks for playing!" )
1923 break
20-
24+
2125 if user_choice not in options :
2226 print ("Invalid choice. Please try again." )
2327 continue
24-
28+
2529 computer_choice = random .choice (options )
2630 print (f"Computer chose: { computer_choice } " )
27-
31+
2832 if user_choice == computer_choice :
2933 print ("It's a tie!" )
30- elif (user_choice == "rock" and computer_choice == "scissors" ) or \
31- (user_choice == "scissors" and computer_choice == "paper" ) or \
32- (user_choice == "paper" and computer_choice == "rock" ):
34+ elif (
35+ (user_choice == "rock" and computer_choice == "scissors" )
36+ or (user_choice == "scissors" and computer_choice == "paper" )
37+ or (user_choice == "paper" and computer_choice == "rock" )
38+ ):
3339 print ("You win!" )
3440 else :
3541 print ("You lose!" )
3642 print ("-" * 20 )
3743
44+
3845if __name__ == "__main__" :
39- play_rock_paper_scissors ()
46+ play_rock_paper_scissors ()
0 commit comments