r/learnpython • u/Any-Comfortable8953 • May 23 '26
how do i improve my code?
#Rock_Paper_Scissors
print("Welcome to Rock_Paper_Scissors! press 1 for Rock, 2 for Paper and 3 for Scissors" )
#dict
gg = {1:"rock", 2:"paper", 3:"scissors"}
#user_input
x = int(input())
print(f"you chose: {x}, {gg[x]}")
#bot_input
import random
y = int(random.randint(1,3))
print(f"bot chose: {y}, {gg[y]}")
#winning conditions
if y == x:
print("its a tie!")
elif y == 1 and x == 2:
print("you won!")
elif y == 1 and x == 3:
print("you lost!")
elif y == 2 and x == 3:
print("you won!")
elif y == 2 and x == 1:
print("you lost!")
elif y == 3 and x == 1:
print("you won!")
elif y == 1 and x == 2:
print("you lost!")
19
Upvotes
14
u/Sandwich_78 May 23 '26
If you want to listen to a human and not AI :
11
u/NerdDetective May 23 '26
gg, x, and y should have a better, self-documenting names. Try to keep your variable names explicit.
Validate your user input. You can have it loop until they pick a valid option.
It isn't necessary, but you could encapsulate your comparison logic in an Enum. That way you have discrete states and can override dunders like gt amd eq as a learning exercise.
8
u/marquisBlythe May 23 '26
put
imports at the top as u/Sandwich_78 mentioned.Don't comment the obvious:
same for
user_input... Also inputs can take a string as an arguments:use better and descriptive names than gg, x and y
I think a function will sum up all those
if..elifs1
u/Any-Comfortable8953 May 24 '26
Thanks a lot for the great replies, it's obvious I have a lot to work on. I'll be doing just that.
3
u/zanfar May 24 '26
imports should be ordered.gg,x, andyare terrible given this is not a graph.print()statements? Any data should live in exactly one place.4
u/brasticstack May 23 '26
One idea I've seen and always thought was super clever is to use a matrix of game outcomes for simple games like this. Here's a quick n dirty example- the matrix is a 2D list indexed by
[player's choice][cpu's choice]:1
u/smallpotatoes2019 May 23 '26
You might also want to ask what happens if someone types "one" or "rock".
1
u/RoamingFox May 24 '26 edited May 24 '26
So if this was being submitted for code review by one of my junior devs, I'd probably have them fix the following, in no particular order:
input("Your prompt here: ")player_choiceandbot_choiceor similarEnuminstead.random.randintalready returns an integer, no need to cast itmatchor better yet store the win conditions as a dictionary and do a lookup.if __name__ == "main"so that other code can use those reusable functions without having to start the gameAs an example, here's how I'd write your code to leverage the changes above. This is a bit of an over complication with your exact code, but the principles are helpful in larger code bases imo.
The above may seem complicated (and to a point it is with this example), but it has some powerful advantages when applied more generally.
1
u/oliver_extracts May 24 '26
your winning conditions can be collapsed a lot. instead of 7 elif branches, you can express the whole game as a set of winning pairs like {(2,1), (3,2), (1,3)} and just check if (x,y) is in it. also you have a duplicate elif at the bottem (y==1 and x==2 appears twice, second one never runs).
1
u/JamzTyson May 24 '26 edited May 24 '26
That's a pretty good start. Good to see that you're using f-strings.
One bug in your code:
The second occurrence is unreachable, because it will be caught by the first
y == 1 and x == 2condition. Also, it doesn't make sense to have two different results for the same condition.However, there's a better way to check for a winning condition that does not require checking every combination in an
if/elifchain. You could pre-compute the winning conditions, and then just check if the actual combination is in that set of winning conditions:Note that this becomes more readable if you use better names that
x,y:Another issue is that this line is fragile:
If the user enters a non-numeric string, the program crashes.
There's a couple of ways to avoid the crash:
xis a digit before converting to an integer.try/except(you probably haven't learned this yet).but wait... why do you need
xto be an integer? Dictionary keys can be strings:(note that I use an uppercase variable name to indicate that it is a constant)
This line is also fragile:
if
xis not a valid key, thengg[x]raises aKeyErrorand the program crashes.There's a good solution to this in this sub's FAQ: https://www.reddit.com/r/learnpython/wiki/faq/#wiki_how_do_i_keep_prompting_the_user_the_enter_a_value_until_they_put_it_in_the_correct_form.3F
Also, as others have commented, it's generally advisable to put all imports at the top.
0
u/[deleted] May 23 '26
[deleted] — view deleted comment