[Tutor] What's the invalid syntax?

Alan Gauld alan.gauld at btinternet.com
Wed Jul 26 11:24:27 CEST 2006


"Nathan Pinno" <falcon3166 at hotmail.com> wrote:
> What's the invalid syntax?

Nathan, we have repeatedly asked you to post your
error messages, it really does help. We don't all have the time
nor inclination to have to read through every line of your code
trying to spot an error... Give us a clue, please...the interpreter
does the job for you, just pass on its advice!

Having said that, I see one glaring set of errors:

def menu_choice():
    choice = raw_input("Enter the letter of your choice: ")
    return choice

def date1():

date1 is the name of a function.

def date2():

as is date2

while 1:
    menu()
    menu_choice()

You never assign the return value of the function so choice is not 
set.
You need to reread the tutorial section on functions and return values
and namespaces.

    if choice == A:
        date1()
        days = int(raw_input("Enter the number of days to add: "))
        date3 = date1 + days

But here you try to create a variable which consists of a function
with one added to it. You cannot add numbers to functions.

You should havbe called date1 here

      date3 = date1() + 1

        days = date2 - date1

And here you are subtracting 2 functions. You should have called them 
here:

       days = date2() - date1()

So in essence I think your syntax errors are to do with your using
function names rather than the return values of those functions.
Remember a function returns a value, not a variable.

try this:

>>> def f():
...       x = 42
...      return x
...
>>> print x  #     an error because the name x ionly exists inside f,
                         the function returns the value of x, thus:

>>> print f()
42

and finally, functions are objects:

>>> print f     # <function object>
>>> print f+1  # error message
>>> print f() + 1
43

Or we could assign the result of f to a variable

>>> f_result = f()
>>> print f_result+1
43

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 





More information about the Tutor mailing list