简体   繁体   English

if语句中的Python语法错误

[英]Python Syntax Error in if statement

I'm stucked with a Python if-statement. 我坚持使用Python if语句。 I finished pretty much everything but when running my progarmm, it gives my an syntax error in the first line: I'm so sure I did everything right but as I'm very new to Python and programming at all, it might just be a very foolish mistake... Thanks for helping guys! 我几乎完成了所有工作,但是在运行progarmm时,它在第一行中给了我一个语法错误:我确定我做对了所有事情,但是由于我对Python和编程非常陌生,所以可能只是非常愚蠢的错误...感谢大家的帮助!

if a == 2:   
StartDeckNeighbourright = StartDeck[a + 1]
StartDeckNeighbourright2 = StartDeck[a + 2]

If this isn't the IndentationError that jramirez's answer fixes, but rather an actual SyntaxError , it's probably a problem with the line before the if statement. 如果这不是jramirez的答案所解决的IndentationError ,而是实际的SyntaxError ,则可能是if语句之前的行有问题。

In Python, you can continue an expression across multiple lines, as long as the expression is inside parentheses. 在Python中,只要表达式在括号内,就可以跨多行继续表达式。 So, if you accidentally leave off a ) at the end of a function call, or a tuple, or anything else, you often get a mysterious SyntaxError on the next line. 因此,如果在函数调用,元组或其他任何内容的结尾不小心遗忘了) ,则通常在下一行会收到一个神秘的SyntaxError For example, this code: 例如,此代码:

foo = (1, 2
if a == 2:
    pass

… will give this error: …将给出此错误:

    if a == 2:
             ^
SyntaxError: invalid syntax

And just adding another comma moves the error somewhere different! 仅添加另一个逗号会将错误移到其他地方!

foo = (1, 2,
if a == 2:
    pass

    if a == 2:
     ^
SyntaxError: invalid syntax

Why? 为什么? Well, even when you understand exactly what these errors mean, they still aren't very helpful. 好吧,即使您确切地理解了这些错误的含义,它们仍然不是很有帮助。 So first, remember: 所以首先,请记住:

If you get a SyntaxError on a perfectly valid line, look for a missing ) (or ] or } , or an extra \\ , or a few other special cases) on the line above. 如果在完全有效的行上出现SyntaxError在上面的行中查找缺少的) (或]} ,或多余的\\ ,或其他一些特殊情况)。

And if you can get an editor that helps you match up parentheses and brackets, it will make this problem much less likely. 而且,如果您可以找到一个可以帮助您将括号和括号匹配起来的编辑器,则将大大减少此问题的发生。 (For example, with emacs, at least the way I have it set up, it'll automatically try to indent the if line 7 characters for me, and if I "fix" it it'll fight back against me, and eventually it'll be hard not to notice something is wrong. Then I point at the first ( and it tells me it's unmatched.) (例如,使用emacs,至少按照我的设置方式,它会自动尝试为我缩进if 7行字符,如果我“修复”它会反击我,最终“很难不注意到出了什么问题。然后我指着第一个(它告诉我这是无与伦比的。)

But if you want to know, here goes: 但是,如果您想知道,这里有:

The first version builds a tuple with the value 1 , then a value starting with 2 and continuing onto the next line. 第一个版本使用值1构建一个元组,然后以2开始并继续到下一行的值。 2 if a == 2 is a perfectly good beginning for a ternary if expression, but 2 if a == 2: is not; 2 if a == 2是三元if表达式的一个很好的开始, if 2 if a == 2: ;但是2 if a == 2:不是,则为2 if a == 2: the colon forces it to be an if statement, and you can't put a statement in the middle of an expression. 冒号将其强制为if语句,并且您不能在表达式中间放置一条语句。

The second version builds a tuple with the value 1 , the value 2 , and more values continuing on the next line. 第二个版本将构建一个元组,其值1 ,值2以及更多值在下一行继续。 if cannot be the start of any valid expression, so you get the SyntaxError earlier. if不能作为任何有效表达式的开始,因此您可以较早地获得SyntaxError But still not early enough to be useful, of course. 但是,当然还不够早就有用。

You should post the error you are seeing, however I think all you need is indentation after the if statment 您应该发布所看到的错误,但是我认为您所需要的只是if语句后的缩进

if a == 2:   
    StartDeckNeighbourright = StartDeck[a + 1]
    StartDeckNeighbourright2 = StartDeck[a + 2]
---- four spaces of indentation

In python You must use Indentation: 在python中,您必须使用缩进:

if a == 2:
    StartDeckNeighbourright = StartDeck[a + 1]
    StartDeckNeighbourright2 = StartDeck[a + 2]

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

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