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.
我需要x是一个整数,这样我的下一部分代码就可以工作了,但是当我在0,1或2附近删除引号时,它会说“为计算机输入可读的东西”,我得到了这个错误信息。
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!")
5 个解决方案
#1
3
You should be using elif
here:
你应该在这里使用elif:
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.
否则,所有三个条件都将在每次运行时进行评估。意思是,如果第一个通过,那么x将是第二个整数。
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.
最后,Python不使用分号。
#2
2
You are calling x.lower() after you assign x to an integer.
在将x赋给一个整数之后,您将调用x.lower()。
Also, you should probably not use the same variable for the integer and the input string.
另外,您可能不应该为整数和输入字符串使用相同的变量。
#3
0
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
不要忘记尝试/除非,你将会得到更短更可读的代码。
#4
0
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.
iCodez的答案是一个,但是你应该使用字符串,比如下面的,如果你没有使用数字转换来分解你的打印语句,而不是两个。
Edit: Had to change what y was, oops
编辑:必须改变y的值。
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.
在Python中不需要分号,但是如果它使您感到舒适,您仍然可以使用它们。
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))]
#5
-2
Use raw_input() instead of input.
使用raw_input()而不是输入。
Also this should be:
这应该是:
if x.lower() == "r": x = "0"
如果x。lower() == "r": x = "0"
#1
3
You should be using elif
here:
你应该在这里使用elif:
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.
否则,所有三个条件都将在每次运行时进行评估。意思是,如果第一个通过,那么x将是第二个整数。
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.
最后,Python不使用分号。
#2
2
You are calling x.lower() after you assign x to an integer.
在将x赋给一个整数之后,您将调用x.lower()。
Also, you should probably not use the same variable for the integer and the input string.
另外,您可能不应该为整数和输入字符串使用相同的变量。
#3
0
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
不要忘记尝试/除非,你将会得到更短更可读的代码。
#4
0
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.
iCodez的答案是一个,但是你应该使用字符串,比如下面的,如果你没有使用数字转换来分解你的打印语句,而不是两个。
Edit: Had to change what y was, oops
编辑:必须改变y的值。
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.
在Python中不需要分号,但是如果它使您感到舒适,您仍然可以使用它们。
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))]
#5
-2
Use raw_input() instead of input.
使用raw_input()而不是输入。
Also this should be:
这应该是:
if x.lower() == "r": x = "0"
如果x。lower() == "r": x = "0"