To fulfil the requirements of the instructions, this version of rock, paper, scissors game works by taking a number from the user and the computer to represent their hand. (0 = rock, 1 = paper, 2 = scissors) From the numbers we then get the string representations of their hands. Next, we determine the winner by comparing the string representations of their hands. Lastly, we print out the result of the game to announce the winner.
I tried to make the function determine_winner()
as simple as possible and this is how far I could get. Do you think it is a good idea to use dictionary there? Any comments to improve in general would be appreciated.
import random
user_hand = None
computer_hand = None
winner = None
result = None
def get_hand(num: int) -> str:
"""Get str representation of the user or the computer's hand."""
if num == 0:
return "rock"
elif num == 1:
return "paper"
elif num == 2:
return "scissors"
def determine_winner() -> str:
"""Get str representation of the winner."""
user_win_combinations = {"rock": "scissors", "paper": "rock", "scissors": "paper"}
if user_hand == computer_hand:
winner = "Draw"
elif (user_hand, computer_hand) in user_win_combinations.items():
winner = "You"
else:
winner = "Computer"
return winner
# take in a number 0-2 from the user that represents their choice
print("Rock = 0, paper = 1, scissors = 2")
while True:
user_input = input("Please enter your choice: ")
if user_input in ["0", "1", "2"]:
break
else:
print("Incorrect input!")
# get str representations of user's hand and computer's hand.
user_hand = get_hand(int(user_input))
computer_hand = get_hand(random.randint(0,2))
# determine who wins
winner = determine_winner()
# print out the result
print(f"Your choice ({user_hand}) vs Computer's choice ({computer_hand})")
if winner == "Draw":
result = "It's a draw!"
else:
result = f"{winner} won!"
print(result)
Output examples:
# Draw
Rock = 0, paper = 1, scissors = 2
Please enter your choice: 1
Your choice (paper) vs Computer's choice (paper)
It's a draw!
# User wins
Rock = 0, paper = 1, scissors = 2
Please enter your choice: 2
Your choice (scissors) vs Computer's choice (paper)
You won!
# Computer wins
Rock = 0, paper = 1, scissors = 2
Please enter your choice: 2
Your choice (scissors) vs Computer's choice (rock)
Computer won!