Python在此代码上抛出SyntaxError
请帮助您正在使用Python在此代码上抛出SyntaxError,python,dictionary,Python,Dictionary,请帮助您正在使用input()而不是raw_input();这将输入解释为Python表达式。很容易引发SyntaxError异常: 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
input()
而不是raw_input()
;这将输入解释为Python表达式。很容易引发SyntaxError异常:
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;
你可能想扭转这两个问题:
dic[raw_input("Enter the value you want to store: ")] = raw_input("Enter the access key of a value: ")
Python将首先请求值。如果需要先请求密钥,请先将其存储在单独的变量中:
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: ")
不是
所以完整的代码应该是:
dic[raw_input("Enter the value you want to store: ")] = input("Enter the access key of a value: ")
它在哪里抛出语法错误?错误消息的完整副本是什么?什么Python版本?我无法在Python2.7上重现该错误。@FrédéricHamidi:我暂时忽略了这些错误,因为它们不是语法错误的原因。抱歉,各位,我的同事,这段代码没有语法错误,因为它通过了我的解释器。因此,如果在其他地方有错误,则不在此范围内code@KanhaPrasad请在发帖前花些时间和精力适当地解释您的问题,然后再发帖一个不是语法错误原因的问题。@MartijnPieters我可以做得非常好。这就是我的答案所在。@MartijnPieters它产生语法错误的原因是提示具有误导性,导致用户键入在实际上下文中无效的内容。输入/原始输入的使用与此无关。之后,您仍然需要
ast.l\u eval
第二个输入的字符串…您无法使用raw\u input()
提示符生成SyntaxError
。
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;