Syntax error with if statement in python 2.7

2024/11/16 9:55:00

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 179if 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.

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.

https://en.xdnf.cn/q/118940.html

Related Q&A

Python- Quicksort program in-place

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?#…

Maximum recursion error in `__eq__` implementation

class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate objects x coordinate.# Getter methods are better practice than just accessing an attr…

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…

cnn news webscraper return empty [] without information

so i wrote this code for now: from urllib import request from bs4 import BeautifulSoup import requests import csv import reserch_term = input(What News are you looking for today? )url = fhttps://editi…

Why the code shows all the addition process?

Code: sum=0 for i in range(10,91):sum=sum+iprint(sum)When I wrote this code, the answer was Output: 10 21 33 46 60 75 91 108 126 145 165 186 208 231 255 280 306 333 361 390 420 451 483 516 550 585 621 …

Creating a list of keywords by scrolling through a dataframe (python)

I have a dataframe that looks like this: dataFrame = pd.DataFrame({Name: ((" Verbundmrtel , Compound Mortar , Malta per stucchi e per incollaggio "),(" StoLevell In Absolute , StoLeve…

How to click this button with python selenium

Im looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.Im having trouble identifying the button to…

How to find the average of numbers being input, with 0 breaking the loop?

I just need to figure out how to find the average of all these input numbers by the user while using 0 as a exit of the loop. I need to figure out how to eliminate using 0 as part of the average. examp…

NoSuchElementException when loading code using Selenium on Heroku

Error: ERROR:asyncio:Task exception was never retrieved2022-03-14T14:08:52.425684+00:00 app[worker.1]: future: <Task finished name=Task-30 coro=<Dispatcher._process_polling_updates() done, define…

Python alphanumeric

Problem:I have to go through text file that has lines of strings and determine about each line if it is alphanumeric or not. If the line is alphanumeric print for example "5345m34534l is alphanume…