我一直试图运行这个程序一段时间,但我似乎无法找出导致错误的原因。
以下是我收到错误的代码行:
from math import *
from myro import *
init("simulator")
def rps(score):
""" Asks the user to input a choice, and randomly assigns a choice to the computer."""
speak("Rock, Paper, Scissors.")
computerPick = randint(1,3)
userPick = raw_input("Please enter (R)ock, (P)aper, or (S)cissors.")
if userPick = R <#This line is where the error shows up at>
print "You picked rock."
elif userPick = P
print "You picked paper."
else
print "You picked Scissors."
score = outcome(score, userPick, computerPick)
return score
答案 0 :(得分:6)
您正在使用赋值运算符而不是相等。此外,您缺少if语句的冒号而不引用字符串。
if userPick == 'R':
...
elif userPick == 'P':
...
else:
...
我注意到你不应该在else
案例中使用'S'
。 'S'
应该是另一个有效条件,否则应该是错误状态catchall。
另一种方法是:
input_output_map = {'R' : 'rock', 'P' : 'paper', 'S' : 'scissors'}
try:
print 'You picked %s.' % input_output_map[user_pick]
except KeyError:
print 'Invalid selection %s.' % user_pick
或者:
valid_choices = ('rock', 'paper', 'scissors')
for choice in valid_choices:
if user_choice.lower() in (choice, choice[0]):
print 'You picked %s.' % choice
break
else:
print 'Invalid choice %s.' % user_choice
答案 1 :(得分:2)
if userPick = R:
应该是
if userPick == "R":