Reputation: 23
I am trying to write a very simple programme for a rock, paper, scissors game. I produce a random integer which is the opponent's move, and then I input my own "guess" in integer form. Then, I combine these two integers into a list and test these coordinates against a results matrix.
I have converted "rock", "paper" and "scissors" into integers as follows:
guess = input("Enter your move: \"rock\", \"paper\", \"scissors\", or \"quit\": \n")
if guess == "rock":
guess = 0
if guess == "paper":
guess = 1
if guess == "scissors":
guess = 2
if guess == "quit":
break
weapon = random.randint(0, 2) # 0 is rock, 1 is paper, 2 is scissors
outputGuess = (int(guess), int(weapon))
Is there a way to get rid of all the if statements? I have tried the following, but it does not work:
guess = input("Enter your move: \"rock\", \"paper\", \"scissors\", or \"quit\": \n")
"rock" = 0
"paper" = 1
"scissors" = 2
weapon = random.randint(0, 2) # 0 is rock, 1 is paper, 2 is scissors
outputGuess = (int(guess), int(weapon))
Thank you!
Upvotes: 2
Views: 121
Reputation: 10709
As @SharathNS suggested, you can try exploring the usage of enums so that everything is defined in 1 place where you would have greater control over the possible options.
from enum import Enum
import random
class GameOption(Enum):
rock = 0
paper = 1
scissors = 2
GAME_OPTION_KEYS = [option.name for option in GameOption]
GAME_OPTION_VALUES = [option.value for option in GameOption]
while (guess := input(f"Enter your move: {GAME_OPTION_KEYS}, or \"quit\": \n")) != "quit":
weapon = random.choice(GAME_OPTION_VALUES)
outputGuess = (GameOption[guess], "vs", GameOption(weapon))
print(outputGuess)
Output
$ python3 script.py
Enter your move: ['rock', 'paper', 'scissors'], or "quit":
paper
(<GameOption.paper: 1>, 'vs', <GameOption.scissors: 2>)
Enter your move: ['rock', 'paper', 'scissors'], or "quit":
paper
(<GameOption.paper: 1>, 'vs', <GameOption.paper: 1>)
Enter your move: ['rock', 'paper', 'scissors'], or "quit":
rock
(<GameOption.rock: 0>, 'vs', <GameOption.scissors: 2>)
Enter your move: ['rock', 'paper', 'scissors'], or "quit":
scissors
(<GameOption.scissors: 2>, 'vs', <GameOption.rock: 0>)
Enter your move: ['rock', 'paper', 'scissors'], or "quit":
rock
(<GameOption.rock: 0>, 'vs', <GameOption.paper: 1>)
Enter your move: ['rock', 'paper', 'scissors'], or "quit":
quit
Upvotes: 1
Reputation: 407
There are already two great answers that would solve the problem but here is my solution. You can put the strings into a list called options
and then get the index of the guess
.
guess = input("Enter your move: \"rock\", \"paper\", \"scissors\", or \"quit\": \n")
options = ["rock", "paper", "scissors"]
if guess in options:
guess = options.index(guess)
else:
break
weapon = random.randint(0, 2) # 0 is rock, 1 is paper, 2 is scissors
outputGuess = (guess, weapon)
I removed the int()
in the outputGuess
line because the randint()
and index()
functions both return an integer.
Upvotes: 1
Reputation: 1834
Alternatively, you could use 1 line if statement
. The syntax is result if condition else result
.
guess = 0 if guess=='rock' else 1 if guess=='paper' else 2 if guess=='scissors' else break
It checks if it's rock
, otherwise check if its paper
, otherwise check if its scissors
, otherwise break
because it's none of the guess
Upvotes: 1
Reputation: 159
You could use a dict to translate it into its integer counterpart.
guess = input("Enter your move: \"rock\", \"paper\", \"scissors\", or \"quit\": \n")
guesses = {'rock':0, 'paper':1, 'scissors':2}
weapon = random.randint(0, 2)
output = (guesses[guess], weapon)
You don't need to change the type of the weapon since it is already an integer.
Upvotes: 0