2012-08-16 40 views
-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 
+0

* *是什麼錯誤?究竟在哪裏?您可以請發佈完整的追溯錯誤消息 – Levon 2012-08-16 18:01:43

+2

這不是一行代碼。 – 2012-08-16 18:01:51

+0

什麼行和錯誤是什麼? – eduffy 2012-08-16 18:02:00

回答

6

您正在使用賦值運算符,而不是平等的。此外,你錯過了你的if語句的冒號,而不是引用你的字符串。

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

我注意到,你不應該爲'S'情況下使用else雖然這裏。 '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 
2
if userPick = R: 

應該

if userPick == "R": 
相關問題