[Tutor] Syntax error help
Evert Rol
evert.rol at gmail.com
Fri Mar 30 22:50:36 CEST 2012
> Alright i have been trying to right a (relatively) simple to calculate area and volume below is my current working code
> def areamenu():
> print 'Square (1)'
> print 'triangle (2)'
> print 'rectangle (3)'
> print 'trapazoid (4)'
> print 'circle (5)'
>
> def squareacalc():
> sidelength = input('enter side length: ')
> print ' the side length is' sidelength ** 2
>
> def displaymenu():
> print 'please make a selection';
> print 'Area (1)';
> choice = input(raw_input('enter selection number'):
You're missing a closing parenthesis here.
But see comments below.
> if (choice == 1):
> Areamenu():
>
> else:
> print 'choice' , choice, ' is wrong try again'
>
> def selctiona():
> Areamenu();
> choicea = input(raw_input'enter selection');
And here you're missing an openening parenthesis.
> if (choicea == 1):
> squareacalc()
>
>
>
> print 'good bye'
>
> I keep getting this error
> Traceback (most recent call last):
> File "<pyshell#3>", line 1, in <module>
> import Area
> File "C:\Python27\Area.py", line 10
> areamenu()
> ^
> SyntaxError: invalid syntax
>
> can anyone tell me what im doing wrong i cant see the problem
> help would be appreciated
A syntax error is often a typing error in the code. In this case, forgotten parentheses, but could be forgotten colons or an unclosed string.
The parentheses problem can often be caught by using a good editor: these often lightlight when you close a set of parentheses, so you can spot syntax errors while you are typing.
There are a number of other things wrong here, though.
Style-wise: Python does not need (and prefers not to have) closing semicolons. In addition, there is no need to surround if statements with parentheses: "if choice == 1:" is perfectly fine and much more legible.
Perhaps you think (coming from another language): "but it doesn't hurt, and I like it this way". But then you're still programming in that other language, and just translating to Python; not actually coding in Python.
Also, this is odd, wrong and pretty bad:
choice = input(raw_input('enter selection number')):
Use either raw_input() (for Python 2.x) or input() (Python 3.x).
It's wrong because you are waiting for input, then use that input as the next prompting string for further input. Like this
>>> choice = input(raw_input('enter selection number'))
enter selection number1
12
>>> print choice
2
(the 12 is the 1 I entered before, plus a 2 I just entered as a second entry.)
So just use one function, and the appropriate one for the Python version.
Lastly, choice will be a string, since input() and raw_input() return a string.
In the if-statement, however, you are comparing that string to an integer. Python does not do implicit conversion, so you'll have to convert the string to an integer first, or compare to a string instead.
(Something similar happens for the sidelength, btw.)
Last thing I see glancing over the code: you define areamenu(), but call Areamenu(). Python is case sensitive, so you'd have to call areamenu() instead.
This may be a bit more information than you asked for, but hopefully you don't mind.
Good luck,
Evert
More information about the Tutor
mailing list