[Tutor] Syntax error help

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] Syntax error help

chris knarvik
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


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

Evert Rol
> 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




_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

Bob Gailer
In reply to this post by chris knarvik
On 3/30/2012 4:26 PM, chris knarvik wrote:
> Alright i have been trying to right a (relatively) simple to calculate
> area and volume below is my current working code

Suggestion: start with a VERY SIMPLE program and get that working. Then
add one new feature at a time.

Is the following in Area.py? If it is then the traceback could not have
come from importing this code, because line 10 does not mention areamenu().
In fact areamenu() appears ONLY in line 1, but not by itself!

> 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
>
What are you using to run the above? I'll guess the interactive window
of IDLE. Try reload(Area). Once a module is imported you must reload to
import it again. (I am assuming you made changes after the initial error.)

The above code should give you something like:
Traceback (most recent call last):
   File "<interactive input>", line 1, in <module>
   File "Script3.py", line 10
     print ' the side length is' sidelength ** 2
                                          ^
SyntaxError: invalid syntax

fix that (do you know what to do?) then you should get a syntax error
for line15. Why is there a : at the end of that line?
then you have 1 more trailing : to deal with.
then there is a missing )
then there is a missing (

Once you fix all the problems then you should see
good bye
since that is the only executable code in Area.py other than def statements.

Suggestion: start with a VERY SIMPLE program and get that working. Then
add one new feature at a time.

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

modulok-2
In reply to this post by chris knarvik
On 3/30/12, chris knarvik <[hidden email]> 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-
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

chris knarvik
In reply to this post by Bob Gailer
Thanks I've fixed

Sent from my iPod

On Mar 30, 2012, at 5:30 PM, bob gailer <[hidden email]> wrote:

> On 3/30/2012 4:26 PM, chris knarvik wrote:
>> Alright i have been trying to right a (relatively) simple to calculate area and volume below is my current working code
>
> Suggestion: start with a VERY SIMPLE program and get that working. Then add one new feature at a time.
>
> Is the following in Area.py? If it is then the traceback could not have come from importing this code, because line 10 does not mention areamenu().
> In fact areamenu() appears ONLY in line 1, but not by itself!
>
>> 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
>>
> What are you using to run the above? I'll guess the interactive window of IDLE. Try reload(Area). Once a module is imported you must reload to import it again. (I am assuming you made changes after the initial error.)
>
> The above code should give you something like:
> Traceback (most recent call last):
>  File "<interactive input>", line 1, in <module>
>  File "Script3.py", line 10
>    print ' the side length is' sidelength ** 2
>                                         ^
> SyntaxError: invalid syntax
>
> fix that (do you know what to do?) then you should get a syntax error for line15. Why is there a : at the end of that line?
> then you have 1 more trailing : to deal with.
> then there is a missing )
> then there is a missing (
>
> Once you fix all the problems then you should see
> good bye
> since that is the only executable code in Area.py other than def statements.
>
> Suggestion: start with a VERY SIMPLE program and get that working. Then add one new feature at a time.
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

Bob Gailer
On 3/30/2012 6:20 PM, [hidden email] wrote:
> Thanks I've fixed
Great. Please share with us your new program and tell us what you do to
run it.

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

Bob Gailer
Please always reply-all so a copy goes to the list.

On 3/30/2012 8:01 PM, chris knarvik wrote:
> that was incomplete it was supposed to be ive fixed most of my
> problems with your help

That's great. Would you post the correct program so we can all learn?
And possibly make other helpful suggestions.

> what do you mean what do i do to run it
There are several ways to execute (run) a Python program.
You can double-click the file in an explorer.
You can at a command prompt type (e.g.) >python Area.py
or you can start an interactive session then type >>>import Area
or you can use an IDE such as IDLE.
   within IDLE you can write the program in an edit window then RUN
   or you can use the interactive window and type >>>import Area
Which of these (or what else) do you do

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

S.Irfan Rizvi

Please remove me from list....i can't do it....they are doing it Attention

On Mar 30, 2012 9:21 PM, "bob gailer" <[hidden email]> wrote:
Please always reply-all so a copy goes to the list.

On 3/30/2012 8:01 PM, chris knarvik wrote:
that was incomplete it was supposed to be ive fixed most of my problems with your help

That's great. Would you post the correct program so we can all learn? And possibly make other helpful suggestions.

what do you mean what do i do to run it
There are several ways to execute (run) a Python program.
You can double-click the file in an explorer.
You can at a command prompt type (e.g.) >python Area.py
or you can start an interactive session then type >>>import Area
or you can use an IDE such as IDLE.
 within IDLE you can write the program in an edit window then RUN
 or you can use the interactive window and type >>>import Area
Which of these (or what else) do you do

--
Bob Gailer
<a href="tel:919-636-4239" value="+19196364239" target="_blank">919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

Bob Gailer
Thank you for posting your code.

Did you forget to reply-all? I am copying this to the list this time.

You apparantly misunderstood my question about what you do to run the program. You said " I import then call the first function  (the displaymenu) or I have it called in the code it does it automatically".

I am running out of patience trying to get from you what we as a Tutor List need in order to help you.

Please answer the following:
What operating system are you using (Windows, Linux, Mac, ...)
WHAT DO YOU DO RUN THE PROGRAM. I gave detailed examples of potential answers (see below). Do you not understand what I'm looking for?

On 3/30/2012 11:22 PM, chris knarvik wrote:
Also here is my revised code with what i call a 'programmers error' let me know  if you  see it because i dont

Why all the imports - you are not using any math, os or sys code. Also You should not do 2 different imports from math. It is better to drop the from ...
import math
import os, sys
from math import *

def displaymenu():
    print 'please make a selection';
    print 'Area (1)';
    choice = raw_input('enter selection number')
OK time to learn how to debug.
Since the code goes from if to print, the if condition (choice == 1) must be False.
What datatype does raw_input give you?
Why would that cause the condition to be False?
One way to answer that is to read the manual regarding raw_input.
Another is to use print statements with type() to tell you the types.
Therefore just before the if put
print type(choice), type(1)

    if choice == 1:
       selctiona()
    else:
        print'choice',choice,'is wrong'
        print"why god wont this thing work"


def areamenu():
    print 'Square (1)'
    print 'triangle (2)'
    print 'rectangle (3)'
    print 'trapazoid (4)'
    print 'circle (5)'

def selctiona():
    areamenu();
    choicea = raw_input('enter selection');
    if choicea == 1:
       squareacalc()

    else:
        print 'why god why'

def squareacalc():
    sidelength = input('enter side length: ')
    print "The Area Is", sidelength **2
   
You don't seem to call reloadarea!
def reloadarea():
   print 'would you like to calculate again'
   choicer = raw_input('yes(1) or no(2)')
   if choicer == 1:
This is called recursion. It is better in this kind of program to put the entire thing in a while loop(see below)*  rather than to use a recursive call.
       displaymenu()
   elif choicer == 2:
       print 'goodbye'
  
displaymenu()

i cant get past the display menu i get this result
please make a selection
Area (1)
enter selection number1
choice 1 is wrong
why god wont this thing work
<module 'Area' from 'C:\Python27\Area.py'>
>>>
let me know if you can see where i went wrong

I hope the guidance I gave above helps.

* Using a while loop instead of recursion:
while True:
  displaymenu()
  print 'would you like to calculate again'
  choicer = raw_input('yes(1) or no(2)')
  if choicer == 2: # this also will not work, for the same reason that choice == 1 does not work. Once you fix choice == 1 then you can also fix this.
    break




what do you mean what do i do to run it
There are several ways to execute (run) a Python program.
You can double-click the file in an explorer.
You can at a command prompt type (e.g.) >python Area.py
or you can start an interactive session then type >>>import Area
or you can use an IDE such as IDLE.
 within IDLE you can write the program in an edit window then RUN
 or you can use the interactive window and type >>>import Area
Which of these (or what else) do you do 


-- 
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

Alan Gauld
In reply to this post by S.Irfan Rizvi
On 31/03/12 03:33, S.Irfan Rizvi wrote:
> Please remove me from list....i can't do it....they are doing it Attention
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Only you could have joined the list and only you can unsubscribe.
Go to the web page and follow the instructions.

If it doesn't work email me and I'll try to figure out what's
going wrong.

--
Alan G
Tutor list moderator
(and just home from vacation...)

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Syntax error help

S.Irfan Rizvi










































































Its failing i can't able to unsubcribe
please remove me from ....its not going to Span either









Error: Authentication failed.

Tutor list: member options login page

In order to change your membership option, you must first log in by giving your email address and membership password in the section below. If you don't remember your membership password, you can have it emailed to you by clicking on the button below. If you just want to unsubscribe from this list, click on the Unsubscribe button and a confirmation message will be sent to you.

Important: From this point on, you must have cookies enabled in your browser, otherwise none of your changes will take effect.

Email address:
Password:

Unsubscribe

By clicking on the Unsubscribe button, a confirmation message will be emailed to you. This message will have a link that you should click on to complete the removal process (you can also confirm by email; see the instructions in the confirmation message).

Password reminder

By clicking on the Remind button, your password will be emailed to you.




On Mar 31, 2012 6:51 PM, "Alan Gauld" <[hidden email]> wrote:
On 31/03/12 03:33, S.Irfan Rizvi wrote:
Please remove me from list....i can't do it....they are doing it Attention
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Only you could have joined the list and only you can unsubscribe.
Go to the web page and follow the instructions.

If it doesn't work email me and I'll try to figure out what's
going wrong.

--
Alan G
Tutor list moderator
(and just home from vacation...)

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor