-3
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;

请帮忙

4

2 回答 2

4

您正在使用input()而不是raw_input(); 这会将输入解释为 Python 表达式。很容易引发 SyntaxError 异常:

>>> 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: ")

Python 将首先询问该值。如果您需要先询问密钥,请先将其存储在单独的变量中:

key = raw_input("Enter the access key of a value: ")
dic[key] = raw_input("Enter the value you want to store: ")
于 2013-05-21T13:13:19.260 回答
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;
于 2013-05-21T13:15:06.207 回答