究竟什么是“无效语法”,为什么我一直在 Python 中得到它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14284446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:58:40  来源:igfitidea点击:

What exactly is "invalid syntax" and why do I keep getting it in Python?

pythonpython-3.x

提问by Hubert Yong

I am trying out this code but I keep running into a problem. I keep getting the message "invalid syntax" and it highlights "maracs" as the syntax error. I've substituted it with several nonexistent words and modified the code but it still gives me the same error. And yes, my variables are random words. Also, I really need the answer to be simple because I can barely understand all the information I find on this on the Internet.

我正在尝试此代码,但一直遇到问题。我不断收到消息“无效语法”,并将“maracs”突出显示为语法错误。我用几个不存在的词替换了它并修改了代码,但它仍然给我同样的错误。是的,我的变量是随机词。另外,我真的需要简单的答案,因为我几乎无法理解我在互联网上找到的所有信息。

start = input("On what day will you be leaving? (1 to 7 representing Monday to Sunday respectively.)"
maracs = input("How many days will your stay be?")
pooper = int(start) + int(maracs)
lob = pooper % 7
if lob = 0:
 print("You will arrive on Day 7 of the week of your arrival.")
else
 print("You will arrive on Day "lob "of the week of your arrival.")

Also, I'm having another problem. I keep getting "NameError" when I am trying this other code out. Please help define "NameError" in a simple way.

另外,我还有一个问题。当我尝试其他代码时,我不断收到“NameError”。请帮助以简单的方式定义“NameError”。

a = All
b = work
d = no
e = play
f = makes
g = Hyman
h = a
i = dull
j = boy

print(a, b, "and", d, e, f, g, h, i, j)

回答by Martijn Pieters

You forgot the closing parenthesis on the preceding line:

您忘记了前一行的右括号:

start = input("On what day will you be leaving? (1 to 7 representing Monday to Sunday respectively.)"

Note that there is no )afterthe closing quote. As python lets you join multiple lines together when using parenthesis, the parser doesn't know anything is wrong until the next line, where you get your SyntaxError because what followed there doesn't make sense.

请注意,在结束引号)之后没有。由于 python 允许您在使用括号时将多行连接在一起,解析器直到下一行才知道有什么问题,在那里您得到 SyntaxError 因为后面的内容没有意义。

As for you second example, you need to put quotes around your strings, Allis not a string but a variable, and you didn't define All:

至于您的第二个示例,您需要在字符串周围加上引号,All它不是字符串而是变量,并且您没有定义All

>>> a = All
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'All' is not defined
>>> a ='All'
>>> a
'All'

回答by HelloUni

You didn't close the bracket (parenthesis) in the first line:

您没有关闭第一行中的括号(括号):

You have to end a function with a bracket. Like input('something'). What you did is: input("On what day will you be leaving..."<-- You forgot the closing parenthesis

你必须用括号结束一个函数。喜欢input('something')。你所做的是:input("On what day will you be leaving..."<-- 你忘记了右括号

start = input("On what day will you be leaving? (1 to 7 representing Monday to Sunday respectively.)")

Edit: And you get the name error in second code because you are trying to print variables which have words. You need to put them in quotations like " "or ' '. When you write them without the " "Python looks for a function/keyword with that name. For e.g; a = "All"

编辑:并且您在第二个代码中遇到名称错误,因为您正在尝试打印包含单词的变量。您需要将它们放在引号中,例如" "' '。当您在没有" "Python 的情况下编写它们时,会查找具有该名称的函数/关键字。例如;a = "All"