I like to copy and paste examples from the teacher in python and play around with the new functions I am learning, but I can't figure out why I am getting an error.
def greet(lang):
if lang=='es':
print('Hola')
elif lang=='fr':
print('Bonjour')
else:
print('hello')
What exactly is "invalid syntax" and why do I keep getting it in Python? - Stack Overflow
Why do I get "SyntaxError: invalid syntax" in a line with ...
How to deal with the error 'invalid syntax'?
"Invalid Syntax" error despite completely valid syntax
You forgot the closing parenthesis on the preceding line:
start = input("On what day will you be leaving? (1 to 7 representing Monday to Sunday respectively.)"
Note that there is no ) after the closing quote. As python lets you join multiple lines together when using parenthesis, the parser doesn't know anything is wrong until the next line, where you get your SyntaxError because what followed there doesn't make sense.
As for you second example, you need to put quotes around your strings, All is not a string but a variable, and you didn't define All:
>>> a = All
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'All' is not defined
>>> a ='All'
>>> a
'All'
You didn't close the bracket (parenthesis) in the first line:
You have to end a function with a bracket. Like input('something'). What you did is: input("On what day will you be leaving..." <-- You forgot the closing parenthesis
start = input("On what day will you be leaving? (1 to 7 representing Monday to Sunday respectively.)")
Edit:
And you get the name error in second code because you are trying to print variables which have words. You need to put them in quotations like " " or ' '. When you write them without the " " Python looks for a function/keyword with that name.
For e.g; a = "All"
For earlier versions of Python(1), an error may be reported on a line that appears to be correct. In that case, you should try 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 was hidden by the first); or
- The previous line has a problem which is being carried forward.
The latter is more likely, especially if commenting out the new offending line causes the error to move again.
For example, consider code like the following, saved as prog.py:
xyzzy = (1 +
plugh = 7
Python 3.8.10 will report an error on line 2, even though the problem is clearly caused by line 1:
pax> python3.8 prog.py
File "prog.py", line 2
plugh = 7
^
SyntaxError: invalid syntax
The code in your question has a similar problem: the code on the previous line to the reported error 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.
(1) For what it's worth, the new PEG parser introduced in Python 3.9 paved the way for much improved error messages (gradually improving from 3.10 thru 3.12). This includes correctly identifying in the source code where the error is:
pax> python3 prog.py
File "prog.py", line 1
xyzzy = (1 +
^
SyntaxError: '(' was never closed
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 ).