I give it a 0 because I tied the bot and the bot still wins.
Just kidding.
Without considering some of the ways this could be made simpler and more compact (ie. strictly from a style guide point of view), this would be rated exactly a 4.88/10 (at least, that's what pylint
is claiming):
************* Module rps
rps.py:3:13: C0303: Trailing whitespace (trailing-whitespace)
rps.py:6:47: C0303: Trailing whitespace (trailing-whitespace)
rps.py:7:13: C0303: Trailing whitespace (trailing-whitespace)
rps.py:9:19: C0326: No space allowed before bracket
for tries in range (1,4):
^ (bad-whitespace)
rps.py:9:21: C0326: Exactly one space required after comma
for tries in range (1,4):
^ (bad-whitespace)
rps.py:13:25: C0326: Exactly one space required after comma
choices = ['Rock','Paper','Scissors']
^ (bad-whitespace)
rps.py:13:33: C0326: Exactly one space required after comma
choices = ['Rock','Paper','Scissors']
^ (bad-whitespace)
rps.py:15:19: C0326: Exactly one space required before assignment
bot_guess = random.choice(choices) #Will randomly pick from the list 'choices'
^ (bad-whitespace)
rps.py:17:0: C0301: Line too long (106/100) (line-too-long)
rps.py:21:110: C0303: Trailing whitespace (trailing-whitespace)
rps.py:21:0: C0301: Line too long (110/100) (line-too-long)
rps.py:32:22: C0303: Trailing whitespace (trailing-whitespace)
rps.py:35:23: C0303: Trailing whitespace (trailing-whitespace)
rps.py:38:22: C0303: Trailing whitespace (trailing-whitespace)
rps.py:41:23: C0303: Trailing whitespace (trailing-whitespace)
rps.py:44:23: C0303: Trailing whitespace (trailing-whitespace)
rps.py:45:58: C0303: Trailing whitespace (trailing-whitespace)
rps.py:59:37: C0303: Trailing whitespace (trailing-whitespace)
rps.py:1:0: C0114: Missing module docstring (missing-module-docstring)
rps.py:6:0: C0103: Constant name "user_point" doesn't conform to UPPER_CASE naming style (invalid-name)
rps.py:7:0: C0103: Constant name "bot_point" doesn't conform to UPPER_CASE naming style (invalid-name)
-----------------------------------
Your code has been rated at 4.88/10
It is OK to ignore the last 2 messages. Pylint thinks variable that are declared outside functions are supposed to be constants. But in this case the script is so short that creating functions doesn't make sense.
- formatting on the trailing comments:
bot_guess = random.choice(choices) #Will randomly pick from the list 'choices'
while user_guess not in choices:#if the user tries to put in anything other then the choices given
much nicer to do: <code><one space>#<one space><comment>
bot_guess = random.choice(choices) # will randomly pick from the list 'choices'
while user_guess not in choices: # if the user tries to put in anything other then the choices given
- The
if-elif
statements used to determine the winner can be simplified quite a bit. Think about it like this: there are only 3 outcomes, so you should only need 3 checks, at most. There are several ways to do it, but here is one I thought of:
if user_guess == bot_guess:
print('Tie!')
elif choices.index(user_guess) == (choices.index(bot_guess) + 2) % 3:
print('The bot earns a point!')
bot_point += 1
else:
print('The user earns a point')
user_point += 1
The list choices = ['Rock','Paper','Scissors']
is treated as a constant, so declare it at the top of the file, right after the import statements (and it actually should be in all UPPER CASE). Since the content won't change, it would be good practice to use a tuple instead of a list (just swap the square brackets [...]
for parens (...)
).
Python 3.4 introduced a feature called f-strings that make string formatting a lot cleaner. Read the link for all †he details, but the main takeaway from it is that if you prefix a string literal with an f
, it allows for string interpolation "on the fly":
print('After ' + str(tries) + ' tries. ' + ' The score is') # NO!!!
print(f'After {tries} tries, the score is') # YES!!!!
- The
try-except
block is not needed. You can replace it with:
user_guess = ''
bot_guess = random.choice(choices)
while True:
user_guess = input('Rock Paper Scissors? ')
if user_guess in choices:
break
else:
print("you must pick a choice from the list!")
- You could also combine the
user_point
and bot_point
into a single points
variable, and assign negative points for the bot and positive points for the user. This is kind of like taking the users side: you earn points by winning, and lose them when the bot wins. The downside is that you can't keep track of how many points each player has earned, only the difference between the 2.
So in the end, I get this:
#!/usr/bin/env python3
import random
CHOICES = ('Rock', 'Paper', 'Scissors')
points = 0
print('Lets play Rock Paper Scissors')
for tries in range(1, 4):
user_guess = ''
bot_guess = random.choice(CHOICES)
while True:
user_guess = input('Rock Paper Scissors? ')
if user_guess in CHOICES:
break
else:
print("you must pick a choice from the list!")
print(f"The bot did {bot_guess}")
if user_guess == bot_guess:
print('Tie!')
elif CHOICES.index(user_guess) == (CHOICES.index(bot_guess) + 2) % 3:
print('The bot earns a point!')
points -= 1
else:
print('The user earns a point!')
points += 1
if points > 0:
print(f'After {tries} tries, THE USER IS THE WINNER BY {points}!!!')
elif points == 0:
print(f'After {tries} tries, IT\'S A TIE!!!')
else:
print(f'After {tries} tries, THE BOT IS THE WINNER BY {points * -1}!!!')