user2212485
user2212485

Reputation: 63

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.

While I can run this script with no issues as a file I just cannot include the last print statement in 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

Upvotes: 0

Views: 187

Answers (4)

user2212485
user2212485

Reputation: 63

As others have kindly pointed out python IDLE only allows one block of code to be executed at a time. In this instance the while loop is a 'block'. The last print statement is outside this block thus it cannot be executed.

Upvotes: 0

khelili miliana
khelili miliana

Reputation: 3822

You have an undentation error in line 10, then you need just to add an 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')

Upvotes: 0

John Coleman
John Coleman

Reputation: 51988

You can't paste more than one statement at a time into IDLE. The problem has nothing to do with indentation. The while loop constitutes one compound statement and the final print another.

The following also has issues when you try to paste at once into IDLE:

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

The fact that this has a problem shows even more clearly that the issue ins't one of indentation.

Upvotes: 1

Robert F.
Robert F.

Reputation: 465

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.

The print at the end however is a new command. You have to run the while loop first and type the print after in the interpreter.

Upvotes: 1

Related Questions