简体   繁体   English

Python IDLE上的缩进错误

[英]Indention Errors on Python IDLE

I'm just following the basic's of python and have been using IDLE as I find it handy to experiment scripts in real-time. 我只是遵循python的基本知识,并且一直在使用IDLE,因为我发现可以方便地进行实时脚本实验。

While I can run this script with no issues as a file I just cannot include the last print statement in IDLE!. 虽然我可以毫无问题地运行此脚本作为文件,但是我不能在IDLE!中包含最后一个打印语句。 I have tried an indentation, 4 spaces, no indentation's. 我尝试了一个缩进,四个空格,没有缩进。 Please explain what I'm doing wrong. 请解释我做错了。

while True:
    print ('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password? (It is a fish.)')
    password = input()
    if password == 'swordfish':
        break
print('value')
SyntaxError: invalid syntax

You can type one statement at a time. 您可以一次输入一个语句。 The while loop considered one with the loop itself, and since the loop is a code block everything in it is okay. while循环将循环本身视为一个循环,并且由于循环是一个代码块,因此一切正常。

The print at the end however is a new command. 但是,最后的print是一个新命令。 You have to run the while loop first and type the print after in the interpreter. 您必须先运行while循环,然后在解释器中键入print

You can't paste more than one statement at a time into IDLE. 您一次不能将多个语句粘贴到IDLE中。 The problem has nothing to do with indentation. 问题与缩进无关。 The while loop constitutes one compound statement and the final print another. while循环构成一个复合语句,而最终print while构成另一个复合语句。

The following also has issues when you try to paste at once into IDLE: 当您尝试一次粘贴到IDLE中时,以下内容也会出现问题:

print('A')
print('B')

The fact that this has a problem shows even more clearly that the issue ins't one of indentation. 有问题的事实更清楚地表明,问题不是缩进之一。

You have an undentation error in line 10, then you need just to add an espace 您在第10行中遇到了undenting错误,那么您只需添加一个espace

while True:
    print ('Who are you?')
    name = input()
    if name != 'Joe':
        continue
    print('Hello, Joe. What is the password? (It is a fish.)')
    password = input()
    if password == 'swordfish':
        break
    print('value')

As others have kindly pointed out python IDLE only allows one block of code to be executed at a time. 正如其他人所指出的那样,python IDLE只允许一次执行一个代码块。 In this instance the while loop is a 'block'. 在这种情况下,while循环是一个“块”。 The last print statement is outside this block thus it cannot be executed. 最后一个打印语句在此块之外,因此无法执行。

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

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