我一直试图运行这个程序一段时间但我似乎无法找出在尝试运行它时导致错误的原因.
这是我收到错误的代码行:
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
Run Code Online (Sandbox Code Playgroud)
您正在使用赋值运算符而不是相等.此外,您缺少if语句的冒号而不引用字符串.
if userPick == 'R':
...
elif userPick == 'P':
...
else:
...
Run Code Online (Sandbox Code Playgroud)
我注意到你不应该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
Run Code Online (Sandbox Code Playgroud)
要么:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
110 次 |
最近记录: |