On 12/8/2025 6:16 PM, MRAB wrote:
On 08/12/2025 21:18, Thomas Passin wrote:
On 12/8/2025 2:49 PM, John Smith via Python-list wrote:
Thanks for the tip. I'll do that here and in future games.
In addition to using constants to parameterize the strings, the whole
game can be made simpler and easier to change when you realize that
the if/else block has a regular construction. Here's a sketch of one
way to take advantage of this structure (untested). You should be
able to fill in the elided code. Use lower case to check the input so
a mistake in capitalization doesn't reject the input. That's a simple
courtesy to the user.
import random #Allows for a random input.
ROCK = 'rock'
PAPER = 'paper'
SCISSORS = 'scissors'
TIE = 'Tie'
ULOSE = 'You Lose'
UWIN = 'You Win'
GAME = {
(ROCK, ROCK): TIE,
(ROCK, PAPER): ULOSE,
# ...
}
OPTIONS = (ROCK, PAPER, SCISSORS)
PROMPT = (f'Welcome to the {ROCK}, {PAPER}, {SCISSORS} game. ' +
'Choose one of the three')
# skipping some code
player_choice = input(PROMPT)
player_choice = player_choice.lower()
if player_choice not in OPTIONS:
print(f'{player_choice} is not an allowed input')
continue
result = GAME[(player_choice, choice)]
print(result)
# ...
There's another way of determining who won.
Give each of the choices a value, such as:
OPTION_VALUES = {ROCK: 0, PAPER: 1, SCISSORS: 2}
Look at the difference between the values of what the bot chose and what
the player chose.
There are 5 possible differences, from -2 to +2.
From that difference you can determine who won or whether it's a tie.
Sure, there are lots of possibilities. Using a little arithmetic would
be nice and concise and well-suited to a C program or even assembler.
It's mostly personal taste, and mine is to make the program easy to
write and later on, read.
--
https://mail.python.org/mailman3//lists/python-list.python.org