2012-08-16 5 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

+2

을 게시하십시오. 코드 줄이 아닙니다. –

+0

어떤 라인과 무엇이 오류입니까? – eduffy

답변

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": 
관련 문제