Python在此代码上抛出SyntaxError [英] Python throws SyntaxError on this code
本文介绍了Python在此代码上抛出SyntaxError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
while 1:
dic = {} #empty dictionary将用于存储所有数据
dic [raw_input(输入您想要的值存储:)] = input(输入值的访问键)
ans = raw_input(Exit:e;存储另一个变量:s; Acces a variable:a)
if ans ==e:
break; #exit主循环
elif ans ==s:
continue;
elif ans ==a:
pass;
请帮助
解决方案
您正在使用 input()
而不是 raw_input()
;这将将输入解释为Python表达式。很容易造成一个SyntaxError异常:
>>>输入(输入一个句子)
输入一个句子:Quick Brown Fox
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< stdin> ;模块>
文件< string>,第1行
Quick Brown Fox
^
语法错误:无效语法
使用 raw_input()
:
dic [raw_input(输入要存储的值:)] = raw_input(输入值的访问键)
/ pre>
你可能想转过这两个问题:
dic [raw_input(输入值的访问键:)] = raw_input(输入要存储的值:)
Python将要求首先。如果您需要先询问密钥,请先将其存储在一个单独的变量中:
key = raw_input(输入访问权限一个值的键值:)
dic [key] = raw_input(输入要存储的值:)
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;
Please help
解决方案You are using
input()
instead ofraw_input()
; this interprets the input as a Python expression. It is easy to make that throw a SyntaxError exception:>>> 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
Use
raw_input()
throughout instead:dic[raw_input("Enter the value you want to store: ")] = raw_input("Enter the access key of a value: ")
You probably want to turn these two questions around:
dic[raw_input("Enter the access key of a value: ")] = raw_input("Enter the value you want to store: ")
Python will ask for the value first. If you need to ask for the key first, store it in a separate variable first:
key = raw_input("Enter the access key of a value: ") dic[key] = raw_input("Enter the value you want to store: ")
这篇关于Python在此代码上抛出SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文