Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me.

Here's the sample code:

another_answer = int(raw_input("> ")

if another_answer == 1:
    print "The pirate's body slowly goes limp and he whispers to you......."
    print "Good luck my son.  You might need this."
    get_gold()

    print: "Do you want to continue to the next room?"

When I run this in the shell I get the following:

File "ex36.py", line 179
  if another_answer == 1:
                        ^
SyntaxError: invalid syntax

I found this bizarre, and after removing the colon I got an error message on the next line:

File "ex36.py", line 180
print "The pirate's body slowly goes limp and he slowly whispers to you......."
     ^
SyntaxError: invalid syntax

Similar questions I've found on stackoverflow center around improper spacing and indentation, but I've looked that over and everything seems to be well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
126 views
Welcome To Ask or Share your Answers For Others

1 Answer

The preceding line is missing a closing parenthesis:

another_answer = int(raw_input("> ")
#                   ^         ^    ^^
#                             --//
#                     -----------?

Python allows expressions to cross physical lines when enclosed in parentheses, so when you don't close parentheses Python continues to look for the rest of the expression. By the time it reaches the : the expression makes no sense anymore and a SyntaxError exception is raised.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...