Re: Rock Paper Scissors Code Bug?
MRAB <[email protected]> Mon, 8 Dec 2025 23:16:06 +0000
| Newsgroups | gmane.comp.python.general |
|---|---|
| Message-ID | <[email protected]> |
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.
--
https://mail.python.org/mailman3//lists/python-list.python.org