Tutorial problem
Steve Holden
steve at holdenweb.com
Mon Dec 27 12:11:16 EST 2004
More information about the Python-list mailing list
Mon Dec 27 12:11:16 EST 2004
- Previous message (by thread): Tutorial problem
- Next message (by thread): SimpleHTTPServer, queries unhandled?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Rÿffffe9veillÿffffe9 wrote:
> Hello,
>
> I have just started doing the python tutorials and i
> tried to modify one of the exercises, it has to to
> with defining functions.
>
> I wanted the user to be able to enter an option and
> then get a print of the selected option. I also wanted
> to have an exit for the user.
>
> This is the code....
>
>
> def PU():
> print 'To have a ...'
>
> def Python():
> print 'To create a programme ...'
>
> def overall():
> print 'To make .....'
>
> def motv():
> print 'I know you can do it!'
>
>
> def menu():
> print ' GOALS IN... '
> print '____________________'
> print '1.Pick up'
> print '2.Python Programming'
> print '3.Overall'
> print '4.Motivation'
> print '5.Exit'
> print '____________________'
>
> menu()
>
>
> while choice != 5:
> choice = input ('Pick a number:')
> if choice == 1:
> PU()
> elif choice == 2:
> Python()
> elif choice == 3:
> overall()
> elif choice == 4:
> motv()
> else:
> print 'Bye'
>
>
>
> The problem is that it doesnt print the
>
> [ choice = input ('Pick a number:') ]
>
> command. It just runs thru the whole thing without
> allowing the user a selection.
>
No, it doesn't. It prints:
Traceback (most recent call last):
File "test97.py", line 24, in ?
menu()
NameError: name 'menu' is not defined
There's a good reason for this, too: you define motv(), and inside that
function you define the menu() function. Since the menu() function is
defined inside the body of motv(), its definition is only created when
motv() is callinside the *local* namespace of the invocation of motv().
ed. The call to motv() returns, and everything the function "knew" is
forgotten.
I suggest you change the indentation of the menu() definition so it's at
the same level as your other functions.
That was a lucky problem, however, because it stopped a later error from
occurring. That "while choice != 5" will fail the first time it is
executed, since you haven't actually set the value of choice to be anything.
Now, quite why you chose to misinform us as to the behavior of your
program I can't really divine. I'll be charitable, and assume that you
are actually referring to some earlier version. But a sound rule for
getting help is "always post the code AND the error traceback".
Also, note that when you type in the digit 1 in response to your
program's prompt (when you eventually see it), that will become the
string value "1" in the choice variable. Since "1" is not equal to 1 you
will always "fall off the end" and print "Bye".
Perhaps you'd like to try again after you've attempted to remedy some of
the deficiencies I have pointed out? There's plenty of help available
here, and you aren't far from a working program.
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
- Previous message (by thread): Tutorial problem
- Next message (by thread): SimpleHTTPServer, queries unhandled?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list