I'm using Notepad ++ to write my code but I keep getting an Invalid Syntax error on a line that doesn't extra.
The code stops at line 54 and it's saying the error is on line 55.
I'm assuming this is a copy and paste related error, but I can't seem to find a way to fix it.
Any suggestions would be great.
Edit - code on line 53 / 54;
city = hashmap.get(cities,'TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city
The error I'm getting is;
File ex38.py, line 55
^
SytanError: invalid sytanx
You forgot to close a set of parentheses, or possibly a multiline string. Or you had a statement that takes a block (like if x:) on the last line and didn't follow it with anything. But most likely the parens. Somewhere there is a ( without a corresponding ).
In the same line you are executing two instructions ,assigning and printing a variable .I think it won't work. Use newline for print statement
Related
I have this code:
def Psat(self, T):
pop= self.getPborder(T)
boolean=int(pop[0])
P1=pop[1]
P2=pop[2]
if boolean:
Pmin = float(min([P1, P2]))
Pmax = float(max([P1, P2]))
Tr=T/self.typeMolecule.Tc
w=0.5*(1+scipy.tanh((10**5)*(Tr-0.6)))
fi1=0.5*(1-scipy.tanh(8*((Tr**0.4)-1)))
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2) # error here
solution = scipy.optimize.newton(funcPsat,guess, args=(T,self))
On the marked line of code, guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2), I get an error message: SyntaxError: invalid syntax.
Pmin, Pmax, w, fi1 and fi2 have all been assigned at this point, so why is there an error?
When I remove that line from the code, the same error appears at the next line of code, again for no apparent reason.
When an error is reported on a line that appears correct, try removing (or commenting out) the line where the error appears to be. If the error moves to the next line, there are two possibilities:
Either both lines have a problem (and the second may have been hidden by the first); or
The previous line has a problem which is being carried forward.
The latter is more likely, especially if removing another line causes the error to move again.
For example, code like the following, saved as twisty_passages.py:
xyzzy = (1 +
plugh = 7
will produce an error on line 2, even though the problem is clearly caused by line 1:
File "twisty_passages.py", line 2
plugh = 7
^
SyntaxError: invalid syntax
The code in the question has a similar problem: the code on the previous line has unbalanced parentheses. Annotated to make it clearer:
# open parentheses: 1 2 3
# v v v
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
# ^ ^
# close parentheses: 1 2
There isn't really a general solution for this - the code needs to be analyzed and understood, in order to determine how the parentheses should be altered.
You're missing a close paren in this line:
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
There are three ( and only two ).
I encountered a similar problem, with a syntax error that I knew should not be a syntax error. In my case it turned out that a Python 2 interpreter was trying to run Python 3 code, or vice versa; I think that my shell had a PYTHONPATH with a mixture of Python 2 and Python 3.
I noticed that invalid syntax error for no apparent reason can be caused by using space in:
print(f'{something something}')
Python IDLE seems to jump and highlight a part of the first line for some reason (even if the first line happens to be a comment), which is misleading.
I have this code:
def Psat(self, T):
pop= self.getPborder(T)
boolean=int(pop[0])
P1=pop[1]
P2=pop[2]
if boolean:
Pmin = float(min([P1, P2]))
Pmax = float(max([P1, P2]))
Tr=T/self.typeMolecule.Tc
w=0.5*(1+scipy.tanh((10**5)*(Tr-0.6)))
fi1=0.5*(1-scipy.tanh(8*((Tr**0.4)-1)))
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2) # error here
solution = scipy.optimize.newton(funcPsat,guess, args=(T,self))
On the marked line of code, guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2), I get an error message: SyntaxError: invalid syntax.
Pmin, Pmax, w, fi1 and fi2 have all been assigned at this point, so why is there an error?
When I remove that line from the code, the same error appears at the next line of code, again for no apparent reason.
When an error is reported on a line that appears correct, try removing (or commenting out) the line where the error appears to be. If the error moves to the next line, there are two possibilities:
Either both lines have a problem (and the second may have been hidden by the first); or
The previous line has a problem which is being carried forward.
The latter is more likely, especially if removing another line causes the error to move again.
For example, code like the following, saved as twisty_passages.py:
xyzzy = (1 +
plugh = 7
will produce an error on line 2, even though the problem is clearly caused by line 1:
File "twisty_passages.py", line 2
plugh = 7
^
SyntaxError: invalid syntax
The code in the question has a similar problem: the code on the previous line has unbalanced parentheses. Annotated to make it clearer:
# open parentheses: 1 2 3
# v v v
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
# ^ ^
# close parentheses: 1 2
There isn't really a general solution for this - the code needs to be analyzed and understood, in order to determine how the parentheses should be altered.
You're missing a close paren in this line:
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
There are three ( and only two ).
I encountered a similar problem, with a syntax error that I knew should not be a syntax error. In my case it turned out that a Python 2 interpreter was trying to run Python 3 code, or vice versa; I think that my shell had a PYTHONPATH with a mixture of Python 2 and Python 3.
I noticed that invalid syntax error for no apparent reason can be caused by using space in:
print(f'{something something}')
Python IDLE seems to jump and highlight a part of the first line for some reason (even if the first line happens to be a comment), which is misleading.
print("inventory[", start,":", finish, "] is", end=" ")
This line of code has my program stuck. It didn't like the spacing so I eliminated it and now it is flagging the colon as invalid syntax. It is straight from my textbook and is a lesson about slicing lists. What am I missing?
For me this code works perfectly if start and finish have been defined.
This error can originate from a SyntaxError in the line before the print. Most certainly you are missing a parens or a bracket.
As an example consider the following code:
print(42 # closing parens intentinally missing here
print(23)
When executed this raises the following error:
File "foo.py", line 2
print(23)
^
SyntaxError: invalid syntax
As you can see the SyntaxError shows one line after the actual error. I suggest you check the line before your print statement.
I have this code:
def Psat(self, T):
pop= self.getPborder(T)
boolean=int(pop[0])
P1=pop[1]
P2=pop[2]
if boolean:
Pmin = float(min([P1, P2]))
Pmax = float(max([P1, P2]))
Tr=T/self.typeMolecule.Tc
w=0.5*(1+scipy.tanh((10**5)*(Tr-0.6)))
fi1=0.5*(1-scipy.tanh(8*((Tr**0.4)-1)))
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2) # error here
solution = scipy.optimize.newton(funcPsat,guess, args=(T,self))
On the marked line of code, guess = Pmin+(Pmax-Pmin)*((1-w**2)*fi1+(w**2)*fi2), I get an error message: SyntaxError: invalid syntax.
Pmin, Pmax, w, fi1 and fi2 have all been assigned at this point, so why is there an error?
When I remove that line from the code, the same error appears at the next line of code, again for no apparent reason.
When an error is reported on a line that appears correct, try removing (or commenting out) the line where the error appears to be. If the error moves to the next line, there are two possibilities:
Either both lines have a problem (and the second may have been hidden by the first); or
The previous line has a problem which is being carried forward.
The latter is more likely, especially if removing another line causes the error to move again.
For example, code like the following, saved as twisty_passages.py:
xyzzy = (1 +
plugh = 7
will produce an error on line 2, even though the problem is clearly caused by line 1:
File "twisty_passages.py", line 2
plugh = 7
^
SyntaxError: invalid syntax
The code in the question has a similar problem: the code on the previous line has unbalanced parentheses. Annotated to make it clearer:
# open parentheses: 1 2 3
# v v v
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
# ^ ^
# close parentheses: 1 2
There isn't really a general solution for this - the code needs to be analyzed and understood, in order to determine how the parentheses should be altered.
You're missing a close paren in this line:
fi2=0.460*scipy.sqrt(1-(Tr-0.566)**2/(0.434**2)+0.494
There are three ( and only two ).
I encountered a similar problem, with a syntax error that I knew should not be a syntax error. In my case it turned out that a Python 2 interpreter was trying to run Python 3 code, or vice versa; I think that my shell had a PYTHONPATH with a mixture of Python 2 and Python 3.
I noticed that invalid syntax error for no apparent reason can be caused by using space in:
print(f'{something something}')
Python IDLE seems to jump and highlight a part of the first line for some reason (even if the first line happens to be a comment), which is misleading.
I'm a beginner at python with experience in Java having to write a python module for a Virtual robot challenge for my high schools MESA(A technology based competition) club. I have been trying to solve this weird problem for days and I only have 6 hours left to fix all the bugs in my code! The "invalid syntax" always occurs on the first line after a while loop here is the relevant information:
Keep in mind that values have been properly added to the lists
Relevant code:
interestlengthl=list()
interestlengthr=list()
interestpoint=list()
def do_examine(robot):
examinecount=0;
while (examinecount<(max(interestpoint)) <-the while loop
i=2+2 <-a innocent line used as an example, this returned an invalid syntax
maxpoint=max(interestpoint)
tomove=(currentposition-(max[interestpoint]-interestpoint(examinecount)))
robot.step_forward(tomove)
leftscan=robot.sense_steps(robot.SENSOR_LEFT)
rightscan=robot.sense_steps(robot.SENSOR_RIGHT)
if (rightscan==interestlengthr(examinecount):
robot.turn_right()
do_rowscan(robot)
if (leftscan==interestlengthl(examinecount):
robot.turn_left()
do_rowscan(robot)
examinecount+=1
robot.turn_right(2)
currentposition=robot.sense_steps(robot.SENSOR_FORWARD)
robot.turn_right(2)
Relevant error:
File "L:\controllers\controller_zigzag.py", line 35
i=2+2
^
SyntaxError: invalid syntax
While loops should have a colon on them, such as with:
while examinecount < max (interestpoint):
just like your if statements further down. And, as an aside, it's not C - you don't need parentheses around the entire conditional.
The syntax error is that you must place a colon after the loop, AND you have unbalanced brackets: while (examinecount<(max(interestpoint)) -> while (examinecount<(max(interestpoint))):