Im a beginner in python and just started making little games and projects like this rock paper scissor one. Any advice to write this shorter or any better? I got kicked off of stack overflow for a post similar to this lol.
import time
import sys
import random
play_again='yes'
while play_again=='yes' or play_again=='y':
options=['rock','paper','scissors']
a=input('\nChoose [Rock - Paper - Scissors]: ')
print('\nBot is choosing')
for i in range(3):
time.sleep(.5)
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(.5)
bot=random.choice(options)
time.sleep(1)
print('\nThe bot chose: ',bot)
if a=='paper' and bot=='scissors':
print('\nThe bot wins')
if a=='paper' and bot=='rock':
print('\nYou win!')
if a=='paper' and bot=='paper':
print('\nYou and the bot tied!')
if a=='rock' and bot=='paper':
print('\nThe bot wins')
if a=='rock' and bot=='scissors':
print('\nYou win!')
if a=='rock' and bot=='rock':
print('\nYou and the bot tied')
if a=='scissors' and bot=='paper':
print('\nYou win!')
if a=='scissors' and bot=='rock':
print('\nThe bot wins')
if a=='scissors' and bot=='scissors':
print('\nYou and the bot tied')
time.sleep(2)
play_again=input('\nPlay again? Type[Yes or No]: ')
if play_again=='no':
print('\nThank you for playing!')
break
1 Answer 1
Operators in python (and any other language) almost always should be surrounded by spaces, e.g. play_again = 'yes'
instead of play_again='yes'
. This is done to increase readability of the code. Install a linter to highlight such problems for you (or use an IDE with a built-in one, such as PyCharm).
sys.stdout.write('.')
sys.stdout.flush()
This can be replaced by
print('.', end='')
Read more on built-in functions to use them efficiently.
Don't use repeating strings in code, it's easy to make a typo that will break the logic, and python won't tell you, since it doesn't understand what you write in strings. Use constants instead:
ROCK = "rock"
PAPER = "paper"
SCISSORS = "scissors"
...
options = (ROCK, PAPER, SCISSORS)
Avoid long if
ladders. If you repeat something multiple times, turn it into a function:
def determine_match_result(player_choice, bot_choice):
if bot_choice == SCISSORS and player_choice == ROCK or
bot_choice == ROCK and player_choice == PAPER or
bot_choice == PAPER and player_choice == SCISSORS:
print("You win!")
elif player_choice == bot_choice:
print("You and the bot tied")
else
print("The bot wins")
Then just call it from your code. You can extract the game rules into a single line by using a dict:
victory_table = {ROCK: (SCISSORS,), PAPER: (ROCK,), SCISSORS: (PAPER,)}
def determine_match_result(player_choice, bot_choice):
if bot_choice in victory_table[player_choice]:
print("You win!")
elif player_choice == bot_choice:
print("You and the bot tied")
else
print("The bot wins")
And the most important advise: use meaningful names. Names should explain the purpose of the object behind them. I don't know what a
means by looking at this name (should be player_choice
). I can't tell that bot
is a string that represents the bot's choice by looking at this name (should be bot_choice
).
-
1\$\begingroup\$ I found an interesting post on stack overflow for rock paper scissors logic deciding, which does some simple math to find what should be the outcome, stackoverflow.com/questions/69444226/… while it is closed, the answer is useful \$\endgroup\$Illusioner_– Illusioner_2022年11月09日 11:18:03 +00:00Commented Nov 9, 2022 at 11:18
-
\$\begingroup\$ Appreciate all the help @QuasiStellar, this was all just homework for me I wasn't too worried about spacing and variable names lol but thank you for the tip, much appreciated. \$\endgroup\$Austin0x– Austin0x2022年11月09日 14:59:55 +00:00Commented Nov 9, 2022 at 14:59
-
\$\begingroup\$ @Austin0x "I wasn't too worried about spacing and variable names" that's the first thing to be worried about. \$\endgroup\$QuasiStellar– QuasiStellar2022年11月09日 15:58:08 +00:00Commented Nov 9, 2022 at 15:58
-
\$\begingroup\$ @QuasiStellar well the code actually works and I ended up getting an A, teacher understood all of my material and the spacing and variable names weren't a problem whatsoever, thank you though! \$\endgroup\$Austin0x– Austin0x2022年11月09日 21:37:02 +00:00Commented Nov 9, 2022 at 21:37
-
1\$\begingroup\$ @Austin0x congrats on the grade. I suppose the course you're going through doesn't require clean code, but it's a very important skill to learn early on if you plan on ever programming outside of school. \$\endgroup\$QuasiStellar– QuasiStellar2022年11月10日 08:14:49 +00:00Commented Nov 10, 2022 at 8:14