Python help with Rock Paper Scissors

Can someone tell me what is wrong with my code please?

import random

print (‘Rock, Paper, Scissors’)
p1=(input(str(’’‘1=Rock
2=Scissors
3=Paper
Choose 1, 2 or 3:’’’)))

p2=(input(str(random.randint(1,3)))

if p1==1 and p2==1:
print(‘Tie: Rock vs Rock’)

if p1==1 and p2==2:
print (‘You win, Rock crushes Scissors’)

if p1==1 and p2==3:
print (‘You lose, Paper covers Rock’)

if p1==2 and p2==1:
print (‘You lose, Rock crushes Scissors’)

if p1==2 and p2==2:
print (‘Tie, Scissors vs Scisors’)

if p1==2 and p2==3:
print (‘You win, Scissors cut Paper’)

if p1==3 and p2==1:
print (‘You win, Paper covers Rock’)

if p1==3 and p2==2:
print (‘You lose, Scissors cut Paper’)

if p1==3 and p2==3:
print (‘Tie, Paper vs Paper’)

What kind of error do you have? Does the code fail to run (syntax error) or not do what you want it to (semantic error)? More info would make it easier to help you.

2 Likes

I’ll just point out a few issues I noticed, not sure exactly which one is causing your issue but just so you know, your casting a string (Text incased in “” or ‘’) into a string with the str(), you can remove that since its unneeded.
Second, your second input (p2), instead of using the random itself, you prompt the user for input, and prompting them with the message that is the random number it should instead be:
p2 = str(random.randint(1,3)

Now the issue that’s probably causing you the problem with the way the program works is the fact that each if is comparing a variable that is a string (user’s inputs are strings unless casted), to a int, which will always be false, So you need to either change the ints to strings (by encasing them with a “” or ‘’) or cast the variables into int (so p1 needs to become
p1=int(input(’‘1=Rock
2=Scissors
3=Paper
Choose 1, 2 or 3:’’’))
and p2 would become
p2 = random.randint(1, 3)

also the code after the if’s has to be indented (CD removes this indent, you might have that it just doesn’t show)

3 Likes

I would suggest adding a if p1 == 1: print(‘okay it’s 1’) (properly formatted) after you get the input to p1. Then figure out why it’s not printing when you enter a 1. You also have the same problem with p2 (among other issues with that declaration).

Python is dynamically typed and strongly typed. If you’re coming from another language where that’s not the case, it can be confusing. To put it another way, what type of value do you think you’re storing in p1? What type of value are you comparing it to? And for p2, what does the input function do?

Also remember in python (x,y…) is a data type called a tuple, which works like a list (or array in other languages) except its immutable so you can’t change the values. I’m not sure if you want p1 and p2 to be stored as tuples but if you do, then you’ll want to use p1[0] to access the value the user typed. Otherwise you can remove the outermost set of parenthesis on both of your variables.

BTW, are you getting a syntax error popup rather than one logged in the console?

1 Like

I was almost going to tell you that you’re missing a LOT of semicolons, but then I remembered this is python.

this message is brought to you by the java gang :upside_down_face:

4 Likes

That isn’t a tuple though? Its just a single value wrapped in brackets ().
If you write the line
val = (3)
and then type(val)
it will return int, since this is just a order of operations, which is redundant but also won’t become a tuple (You need to list multiple elements for it to be a tuple)

My bad lol. Should have tried it in the shell. Consider my last post a fun fact.

Anyway, @curtissmith003 did you figure out what the problem was?

If you really need a tuple of one element, you can use (1,).

I would recommend using code blocks to format your code, make it easier to read. Use three backticks (on the ~ key) in a row to start a block, then three more to end it. See here for more info.

It’s a fun fact, but also generally part of the problem the OP is experiencing. What type is assigned to the variables and what type are the variables being compared to?

One thing you could think about is adding your information to a dictionary. In python, the dictionary is a key value pair setup where you just reference the one you’re looking for:

>>> testdict = {"1" : "Rock", "2" : "Paper", "3" : "Scissors"}
>>> testdict
{'1': 'Rock', '3': 'Scissors', '2': 'Paper'}
>>> testdict["1"]
'Rock'
>>> testdict[str(input("1, 2, or 3\n"))]
1, 2, or 3
1
'Rock'

Sorry the output is just from the python console, but you can translate anything right of the ‘>>>’ prompt as valid python code.

Also, your P2 line isn’t proper syntax and should be giving you an error I’d assume:

>>> testdict[str(random.randint(1,3))]
'Rock'
>>> testdict[str(random.randint(1,3))]
'Paper'
>>>