2013-05-21 8 views
-2
while 1: 
    dic = {} #empty dictionary which will be used for storing all the data 
    dic[raw_input("Enter the value you want to store: ")] = input("Enter the access key of a value: ") 
    ans = raw_input("Exit:e ; Store another variable : s; Acces a variable: a") 
    if ans=="e": 
     break; #exit the main loop 
    elif ans == "s": 
     continue; 
    elif ans=="a": 
     pass; 

당신은 input() 대신 raw_input()을 사용하는파이썬은

+3

** ** do do es 구문 오류가 발생합니까? 오류 메시지의 전체 사본은 무엇입니까? 무슨 파이썬 버전? Python 2.7에서 오류를 재현 할 수 없습니다. –

+1

@ FrédéricHamidi : 구문 오류의 원인이 아니기 때문에 지금은 무시했습니다. –

+0

imstake 님, 죄송합니다. –

답변

4

도와주세요이 코드에 구문 에러가 발생합니다; 이것은 입력을 파이썬 표현식으로 해석합니다. 대신에 걸쳐

>>> input("Enter a sentence: ") 
Enter a sentence: The Quick Brown Fox 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 1 
    The Quick Brown Fox 
      ^
SyntaxError: invalid syntax 

사용 raw_input() :

dic[raw_input("Enter the value you want to store: ")] = raw_input("Enter the access key of a value: ") 

을 당신은 아마 주위에이 두 가지 질문을 설정하려면 :

dic[raw_input("Enter the access key of a value: ")] = raw_input("Enter the value you want to store: ") 

파이썬은 물어볼 것입니다 그가 구문 에러 예외를 쉽게 만들 수 있습니다 값이 이면 처음으로입니다. 먼저 키를 요청해야하는 경우, 먼저 별도의 변수에 저장 :

key = raw_input("Enter the access key of a value: ") 
dic[key] = raw_input("Enter the value you want to store: ") 
0

당신은 당신의 선이 주변의 잘못된 방법은, 당신이 필요로했다 :

dic[raw_input("Enter the access key of a value: ")] = input("Enter the value you want to store: ") 

하지

dic[raw_input("Enter the value you want to store: ")] = input("Enter the access key of a value: ") 

전체 코드는 다음과 같습니다.

while 1: 
    dic = {} #empty dictionary which will be used for storing all the data 
    dic[raw_input("Enter the access key of a value: ")] = input("Enter the value you want to store: ") 
    ans = raw_input("Exit:e ; Store another variable : s; Acces a variable: a") 
    if ans=="e": 
     break; #exit the main loop 
    elif ans == "s": 
     continue; 
    elif ans=="a": 
     pass; 
+1

이것은 구문 오류의 원인이 아닙니다. –

+0

@MartijnPieters 그래도 완벽하게 실행할 수 있습니다. – HennyH

+0

정확합니다. 그게 내 대답이 말한 것입니다. –