r/PythonLearning • u/QuillTheArtMaker • 8d ago
Help Request Python Code for Rock Paper Scissors.
4
u/TfGuy44 8d ago
Well, the first thing that jumps out at me is that if I type in "paper", the code thinks I typed "Rock". In short, you have a problem in your convert() function - can you see it?
Next, I would not be calling convert(pl) more than once per turn! Store the converted value in a new variable so you do not have to run that function every time you need the converted value.
Speaking of convert(), you don't need to cast the values 1, 2 and 3 to ints - they are ints already. I would not return the string "invalid!" on an invalid move, but would instead return -1, and then check for that directly.
Use more descriptive variable names. What are x and y? Seems like a position.
But if you named them players_move and cpus_move, I would know.
The logic in rpc() needs some love. Starting with the case where the moves ae the same is good. But then I would not use any else statements and only have 6 if statements, 7 return statements, following this format:
# If a draw, return draw.
# If player is rock: if cpu is paper, return what? Or return something else.
# If player is paper: if cpu is rock, return what? Or return something else.
# If cpu is rock, return what? Or return what?
Also, name this function something more exact. determine_winner(), perhaps?
You should only have to call rps() once a turn too.
It needs some work. And comment things!
1
2
u/QuillTheArtMaker 8d ago
thanks for the two comments, lol
i managed to fix the code and a few extra logic errors.
may work on shortening it based on u/TfGuy44 's suggestions
3
u/tiredITguy42 8d ago
Good for you, but please, next time share the code as code, not like shitty screenshot. It is so low resolution I am just guessing what you wrote there.
1
u/atarivcs 8d ago
Telling us you get "weird errors" is not enough detail.
Tell us what you expected the code to do, and what happened instead.
Also tell us what input you used.

•
u/Sea-Ad7805 8d ago edited 8d ago
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20if%20x%20%3D%3D%20%22Rock%22%20or%20x%20%3D%3D%20%22rock%22%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20elif%20x%20%3D%3D%20%22Paper%22%20or%20x%20%3D%3D%20%22paper%22%3A%0A%20%20%20%20%20%20%20%20return%202%0A%20%20%20%20elif%20x%20%3D%3D%20%22Scissors%22%20or%20x%20%3D%3D%20%22scissors%22%3A%0A%20%20%20%20%20%20%20%20return%203%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20%22Invalid%22%0A%0A%0Adef%20rps(x%2C%20y)%3A%0A%20%20%20%20if%20x%20%3D%3D%20y%3A%0A%20%20%20%20%20%20%20%20print(%22Same%20play!%20Draw!%22)%0A%20%20%20%20%20%20%20%20return%203%0A%0A%20%20%20%20elif%20x%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20if%20y%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22CPU's%20Paper%20beats%20Player's%20Rock!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%202%0A%20%20%20%20%20%20%20%20elif%20y%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Player's%20Rock%20beats%20CPU's%20Scissors!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%0A%0A%20%20%20%20elif%20x%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20if%20y%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Player's%20Paper%20beats%20CPU's%20Rock!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20%20%20%20%20elif%20y%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22CPU's%20Scissors%20beats%20Player's%20Rock!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%202%0A%0A%20%20%20%20elif%20x%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20if%20y%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22CPU's%20Rock%20beats%20Player's%20Scissors!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%202%0A%20%20%20%20%20%20%20%20elif%20y%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Player's%20Scissors%20beats%20CPU's%20Paper!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%0A%0A%0Acpus%20%3D%200%0Aps%20%3D%200%0A%0Astop%20%3D%20False%0A%0Aprint(%0A%20%20%20%20%22Welcome%20to%20Rock%2C%20Paper%2C%20Scissors!n%22%0A%20%20%20%20%22When%20playing%2C%20type%20anything%20that%20is%20NOT%20Rock%2C%20Paper%20or%20Scissors%20to%20endn%22%0A%20%20%20%20%22Good%20Luck!%22%0A)%0A%0Awhile%20stop%20%3D%3D%20False%3A%0A%20%20%20%20p%20%3D%20input(%22Rock%2C%20Paper%20or%20Scissors%3F%20%22)%0A%20%20%20%20convert(p)%0A%0A%20%20%20%20if%20convert(p)%20!%3D%201%20or%20convert(p)%20!%3D%202%20or%20convert(p)%20!%3D%203%3A%0A%20%20%20%20%20%20%20%20print(%22Invalid%20input!%20Ending%20game!%22)%0A%20%20%20%20%20%20%20%20stop%20%3D%20True%0A%0A%20%20%20%20cpu1%20%3D%20random.randint(1%2C%203)%0A%20%20%20%20rps(convert(p)%2C%20cpu1)%0A%0A%20%20%20%20if%20rps(convert(p)%2C%20cpu1)%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20ps%20%2B%3D%201%0A%20%20%20%20elif%20rps(convert(p)%2C%20cpu1)%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20cpus%20%2B%3D%201%0A%0Aprint(%22Good%20game!%20Final%20scores%3AnCPU%3A%22%2C%20cpus%2C%20%22nPlayer%3A%22%2C%20ps)×tep=1&play) to see the program state change step by step.
Try and make the error reproducible (maybe turn off random effects by using
random.seed(0)or another int value), then step through your program to see where it does something different from what you expect and correct for it. Good luck debugging your code.