Reputation: 37
i need x to be an integer so my next part of code works, but as soon as i remove quotation marks around 0,1 or 2 where it says "making input readable for computer" i get this error message.
from random import randint
# Input
print("Rock: R Paper: P Scissors: S")
x = input("Please pick your choice: ")
y = randint(0,2)
#Making input readable for computer
if x.lower() == "r":
x = 0;
if x.lower() == "p":
x = "1";
if x.lower() == "s":
x = "2";
print("value entered ", x, "value generated ", y)
if (x == y):
print("It's a draw!")
# Calculating "Who wins?"
if x == 0 and y == 1:
print("Computer wins!")
if x == 0 and y == 2:
print("You won!")
if x == 1 and y == 0:
print("You won!")
if x == 1 and y == 2:
print("Computer wins!")
if x == 2 and y == 0:
print("Computer wins!")
if x == 2 and y == 1:
print("You won!")
Upvotes: -1
Views: 32429
Reputation: 764
iCodez answer is the one, but you should just use the strings, like the following, if you are not using the number conversion to factor your print statement, not both.
Edit: Had to change what y was, oops
x = raw_input("Please pick your choice: ").lower()
y = choice(['r','p','s'])
if (x == y):
print("It's a draw!")
# Calculating "Who wins?"
if x == 'r' and y == 'p':
print("Computer wins!")
elif x == 'r' and y == 's':
print("You won!")
elif x == 'p' and y == 'r':
print("You won!")
elif x == 'p' and y == 's':
print("Computer wins!")
elif x == 's' and y == 'r':
print("Computer wins!")
elif x == 's' and y == 'p':
print("You won!")
Now if you want to go with the convertion to integer then you could just use this:
y = randint(0,2)
if x == "r":
x = 0
elif x == "p":
x = 1
elif x == "s":
x = 2
print ['tie', 'you win', 'they win'][x-y]
And semicolons are not needed in Python, but you can still use them if it makes you comfortable.
Edit: Just for fun.
import random
pick = ['r', 'p', 's']
x = ""
while x not in pick:
x = str(raw_input("r, p, or s? ")).lower()
print ['tie', 'y win', 'y win', 'x win', 'x win'][ord(x)-ord(random.choice(pick))]
Upvotes: 0
Reputation: 3582
With a couple of dictionaries this code will be short and concise:
x_conversion = {'r':0, 'p':1, 's': 2}
x = x_conversion[x.lower()]
or list(in this particular case)
x_conversion=['r', 'p', 's]
x = x_conversion.index(x.lower())
And for winner
winner_choice = {(0,1): 'Computer', (1, 2): 'You', ...}
winner = winner_choice[(x, y)]
Don't forget try/except and you'll have your results in much shorter and more readable code
Upvotes: 0
Reputation:
You should be using elif
here:
if x.lower() == "r":
x = 0
elif x.lower() == "p":
x = 1
elif x.lower() == "s":
x = 2
Otherwise, all three conditions are evaluated with every run. Meaning, if the first passes, then x
will be an integer for the second.
Also, you should write your code like this:
x = x.lower() # Put this up here
if x == "r":
x = 0
elif x == "p":
x = 1
elif x == "s":
x = 2
That way, you don't call str.lower
multiple times.
Lastly, Python does not use semicolons.
Upvotes: 3
Reputation: 421
Use raw_input() instead of input.
Also this should be:
if x.lower() == "r": x = "0"
Upvotes: -2
Reputation: 11935
You are calling x.lower() after you assign x to an integer.
Also, you should probably not use the same variable for the integer and the input string.
Upvotes: 2