[Tutor] Syntax error help

Modulok modulok at gmail.com
Fri Mar 30 23:49:57 CEST 2012


On 3/30/12, chris knarvik <x23chris at gmail.com> wrote:
> 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'):
>     if (choice == 1):
>         Areamenu():
>
>     else:
>         print 'choice' , choice, ' is wrong try again'
>
> def selctiona():
>     Areamenu();
>     choicea = input(raw_input'enter selection');
>     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

Chris,

Your code has numerous problems. First, the only lines that should end in a
colon ':' are lines that define something, i.e. class and function signatures,
etc. Your code has them sprinkled in places where they really don't belong. For
instance:

    choice = input(raw_input('enter selection number'):  #<-- No.

Additionally, python statements are almost never terminated with a semicolon
';'. Your code has those in random places as well. Python statements *can* be
semicolon terminated, but this should *only* be used if there is more than one
statement per line. (Generally a discouraged practice, unless in the case of a
list comprehensions):

    choicea = input(raw_input'enter selection'); #<-- Not needed.

The other problem I see is a lot of mis-matched parentheses. For instance your
code has places where there are two opening parentheses, but only one closing
parenthesis:

    choice = input(raw_input('enter selection number'):

Should be:

    choice = input(raw_input('enter selection number'))

However, the above line is pretty nasty. It's making an unnecessary function
call. Above, its waiting for user input to the 'input' function, and using the
return value of the 'raw_input' function for that input, which itself is
waiting for user input. i.e. an input waiting for an input. It should instead
be something like this:

    choicea = input('enter selection: ')

*If* you decide to use the 'raw_input()' function instead, remember to
type-cast the value that gets entered. By default with 'raw_input()' it is
interpreted as a string and not an integer. Thus, the comparison 'if (choicea
== 1)' wouldn't work with 'raw_input()' expected.

This is a problem as well:

    print ' the side length is' sidelength ** 2

The print statement only takes a single argument, or multiple arguments *if*
they are comma separated, e.g:

    print ' the side length is', sidelength ** 2

However, this whole thing would be better written using string substitution::

    print ' the side length is %s' % (sidelength ** 2)

This is also incorrect:

    Areamenu()

Your code defines a function named 'areamenu', not one named 'Areamenu'.
Remember, python is case sEnsItiVe. e.g: Foo() and foo() are different.

Good luck!
-Modulok-


More information about the Tutor mailing list