Vite
Vite

Reputation: 11

Not printing after if statment

So I have just started learning to use Python, and I am getting a syntax error.

Following the book I am using , here is a simple if statement, followed by a print statement that happens regardless of the if statement.

name = "Doug"
if name == 'Doug' :
    print "Hello, Doug!"
print "How are you today?"

The expected output is:

Hello Doug!
How are you today?

if name != Doug, then the output should be

How are you today?

I've done simple ifs a thousand times in C++ and Java, but with brackets. For some reason, the final print comes back with a syntax error.

I am using Python 2.7.8, not Python 3, and using print or print() gives me the same result.

EDIT: No amount of Newlines in the interpreter version worked, however running the script in a .py file worked flawlessly. For some reason , my book failed to mention this.

Upvotes: 1

Views: 155

Answers (1)

John Bollinger
John Bollinger

Reputation: 180073

Your code works for me if I put it in a .py file and run the file through the interpreter. E.g. python hello.py. If I run the python interpreter interactively, however, then I can reproduce a syntax error at the second print statement.

I think this is just a quirk of interactive mode. I can make it work in interactive mode, too, by putting an extra newline between the two print statements. For what it's worth, the interactive-mode prompting makes me think that it doesn't recognize the end of the if statement until I type that extra newline after it (otherwise, another statement in the if block might follow).

Upvotes: 1

Related Questions