简体   繁体   English

Python 3 语法错误无效语法

[英]Python 3 syntax error invalid syntax

With this code I am trying to generate simple multiplication tables.使用此代码,我试图生成简单的乘法表。 The program should ask for input and multiple that number in a range up to 15 and the generate the multiplication table for the number.该程序应要求输入并将该数字乘以最大 15 的范围,并生成该数字的乘法表。 After the if_name_ == ' main ': line I end up with a syntax error after the colon.在 if_name_ == ' main ': 行之后,我以冒号后的语法错误告终。 I normally program in python 2, so python 3 is a bit new to me but I'm not sure what the difference is.我通常在 python 2 中编程,所以 python 3 对我来说有点新,但我不确定有什么区别。 Below I have listed the short but full code.下面我列出了简短但完整的代码。 Any help would be much appreciated.任何帮助将非常感激。

'''Multiplication Table'''

def multi_table(a):
    for i in range(1,16):
        print(' {0} x {1} = {2} '.format(a, i, a*i))



if_name_ == '_main_':
    a = input('Enter a number: ')
    multi_table(float(a))
if_name_ == '_main_':
    a = input('Enter a number: ')
    multi_table(float(a))

should be :应该 :

if __name__ == "__main__":
    a = input('Enter a number: ')
    multi_table(float(a))

Notice that both variable __name__ and __main__ has two underscores around them and that there must be a space between the if keyword and the start of the condition.请注意,变量__name____main__都有两个下划线,并且 if 关键字和条件的开头之间必须有一个空格。

As @Maroun Maroun said right, it has to be if __name__ == "__main__" .正如@Maroun Maroun 所说,它必须是if __name__ == "__main__" But you wont need it.但你不会需要它。 Just write it at the bottom :只需写在底部:

'''Multiplication Table'''

def multi_table(a):
    for i in range(1,16):
        print(' {0} x {1} = {2} '.format(a, i, a*i))

a = input('Enter a number: ')
multi_table(float(a))

Should work, too.也应该工作。

EDIT: In the official docs :编辑:在官方文档中:

https://docs.python.org/3/library/ main .html https://docs.python.org/3/library/main.html

if __name__ == "__main__":

It might also be possible that the "invalid syntax" is because means you have forgotten to close a parenthesis on the previous line of code. “无效语法”也可能是因为您忘记在前一行代码上加上括号。

In the example provided, (i) there's a space missing after if , and (ii) the symbol's name should have two pairs of underscores: __name__ .在提供的示例中,(i) if后面缺少一个空格,(ii) 符号的名称应该有两对下划线: __name__ If these two corrections resolve your problem, great.如果这两个更正解决了您的问题,那就太好了。

But in case you still have another more mysterious unresolved syntax error, then read on...但是,如果您还有另一个更神秘的未解决语法错误,请继续阅读...

Recently I encountered a very similar error message, and it was hard to root-cause, since the error message seems to point in the wrong direction.最近我遇到了一个非常相似的错误消息,并且很难找到根本原因,因为错误消息似乎指向了错误的方向。 The error seems to indicate that the syntax error lies at the end of the if statement.该错误似乎表明语法错误位于if语句的末尾。 However, with Python 3.x I noticed that in case a parentheses mismatch error happens in a python source file, the error might be reported at the beginning of the next block rather than where the mismatch actually occurs.但是,在 Python 3.x 中,我注意到如果在 python 源文件中发生括号不匹配错误,错误可能会在下一个块的开头而不是实际发生不匹配的位置报告。

It's worth checking for mismatched parentheses in the entire source code file, prior to the if statement.if语句之前检查整个源代码文件中不匹配的括号是值得的。 Check if all your ( s are matched by a closing ) .检查您的所有( s 是否都与关闭匹配)

Same problem.同样的问题。 It shows either main or name is undefined它显示 main 或 name 未定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM