-3

我一直在尝试运行该程序一段时间,但在尝试运行它时似乎无法找出导致错误的原因。

这是我收到错误的代码行:

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
4

2 回答 2

6

您正在使用赋值运算符而不是相等。此外,您的 if 语句缺少冒号,并且没有引用您的字符串。

if userPick == 'R':
    ...
elif userPick == 'P':
    ...
else:
    ...

我会注意到你不应该在这里使用else这种'S'情况。 'S'应该是另一个有效条件,否则应该是错误状态包罗万象。

另一种方法是:

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
于 2012-08-16T18:02:08.540 回答
2
if userPick = R:

应该

if userPick == "R":
于 2012-08-16T18:02:11.427 回答