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.
Related
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
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 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
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
I am very new to python, this is my first program that I am trying.
This function reads the password from the standard input.
def getPassword() :
passwordArray =[]
while 1:
char = sys.stdin.read(1)
if char == '\\n':
break
passwordArray.append(char)
return passwordArray
print (username)
print (URL)
getting this error:
Problem invoking WLST - Traceback (innermost last):
(no code object) at line 0
File "/scratch/aime/work/stmp/wlstCommand.py", line 10
while 1:
^
SyntaxError: invalid syntax
Your indentation is not correct. Your while should be indented the same as the line above it.
Python uses indentation to "separate" stuff and the thing with that is you need to have the same kind of indentation across the file. Having a fixed kind of indentation in the code you write is good practice. You might want to consider a tab or four spaces(The later being the suggestion in the PEP8 style guide)
Python is sensitive and depended on indentation. If it complains "invalid Format", that is better than "invalid syntax".