Python keeps generating syntax errors - python

This line had an error:
print('Already built: ' + str(deletedDirs) + ' servers')
On 't' in the word print.
I deleted this one and then the line below it had a syntax error:
time.sleep(timeToSleep)
On 'e'
And so on.... Help me please. It worked yesterday but today it does not.

The line before the statement is missing a closing parenthesis, or curly or square bracket.
The lines you keep deleting are not the problem, but Python doesn't know this until it discovers that that next line makes no sense when it was looking for a comma or closing parenthesis instead.
Demo; there should be two closing parenthesis:
>>> some_function(str('with additional arg, but missing closing parens')
... print('Oops?')
File "<stdin>", line 2
print('Oops?')
^
SyntaxError: invalid syntax

check the file encoding
check the indents

Related

File "<ipython-input-9-4541b518c2e6>", line 31 soup = BeautifulSoup(driver.page_source,"lxml") ^ SyntaxError: invalid syntax [duplicate]

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.

Invalid Syntax in print

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.

Python: Invalid Syntax error on a line that doesn't exist

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

Invalid syntax after FOR statement

I have this piece of code that splits a string with a comma and prints each element:
prefixes="item1,item2,item3"
for prefix in prefixes.split(","):
print prefix
Now, when I try to execute the code above, I get the following error:
Problem invoking WLST - Traceback (innermost last): (no code object) at line 0 File "example.py", line 21
print:
^ SyntaxError: invalid syntax
I tried chaging the code to the following:
prefixes="item1,item2,item3"
for prefix in prefixes.split(","):
try:
print prefix
But then I get the following error:
try:
^ SyntaxError: invalid syntax
This should be very simple but it seems that everything I put after the FOR statement becomes invalid.
Any help would be appreciated. Thanks!
For your particular script you need to put in an except. Unless you're doing something you're not telling us, this will accomplish the print.
prefixes="item1,item2,item3"
for prefix in prefixes.split(","):
try:
print prefix
except:
pass

unexplained syntax error in for loop

I'm getting a syntax error on the 2nd to last line here, but don't know why. It seems identical to the line 2 lines before it, but for some reason I'm getting a syntax error. I've tried it both with and without a blank line between it and the line before it, with the same results.
## numlist = some list
array_size = 20
for row in xrange(array_size):
for col in xrange(array_size):
if(col<=(array_size-4)):
check(sum(numlist[row][col:col+4])
if(row<=(array_size-4)):
check(sum([numlist[row+i][col] for i in range(4)]))
You are missing a closing ')' for the statement below the first if-statement.
check(sum(numlist[row][col:col+4])
^
should be
check(sum(numlist[row][col:col+4]))
^
Note: Using an editor with the feature that matches/highlights parens is a very useful tool to have and will save you from spending time finding these sort of errors.
check(sum(numlist[row][col:col+4])
Should be:
check(sum(numlist[row][col:col+4]))
note the extra parenthesis at the end

Categories