473,246 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Tutorial problem

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.

__________________________________
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250
Jul 18 '05 #1
3 1292
On Mon, 27 Dec 2004 08:15:51 -0800, R˙ffffe9veill˙ffffe9 wrote:
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.


Are you running it from the command line ? Some editors do not 'work'
properly with command line input (redirection issues).

Jul 18 '05 #2
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.


As it is, your code throws an exception for me (temp.py contains your code):

$ temp.py
Traceback (most recent call last):
File "D:\Steve\temp.py", line 24, in ?
menu()
NameError: name 'menu' is not defined

So, I dedented the "def menu" line, which solved the above NameError and
gave me a new exception:

$ temp.py
GOALS IN...
____________________
1.Pick up
2.Python Programming
3.Overall
4.Motivation
5.Exit
____________________
Traceback (most recent call last):
File "D:\Steve\temp.py", line 27, in ?
while choice != 5:
NameError: name 'choice' is not defined

So, at the beginning of the while loop, 'choice' is not yet defined. So
let's define it:

choice = 1
while choice != 5:
...

$ temp.py
GOALS IN...
____________________
1.Pick up
2.Python Programming
3.Overall
4.Motivation
5.Exit
____________________
Pick a number:2
To create a programme ...
Pick a number:1
To have a ...
Pick a number:5
Bye

Seems to be working now. Moral of the story here is that posting the
exact text of the error you got is the best way to get an answer quickly
-- the experienced Python programmers on this list can quickly tell you
what the problem is if given enough information.

As a side note, I would probably write your code as:

print ' GOALS IN... '
print '____________________'
print '1.Pick up'
print '2.Python Programming'
print '3.Overall'
print '4.Motivation'
print '5.Exit'
print '____________________'

def choice_iter():
while True:
choice = int(raw_input('Pick a number: '))
if choice == 5:
break
yield choice

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!'

selections = [PU, Python, overall, motv]
for choice in choice_iter():
selections[choice - 1]()
print 'Bye'

This cleans up the program logic in two ways:

(1) The iteration over user input is modularized into the choice_iter
function. This separates the code reading input from the user from the
code calling the specified functions.

(2) Rather than an set of if-else statements, I put the functions to be
called into a list, and use the integer selected as an index to this list.

HTH!

Steve
Jul 18 '05 #3
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: binnyva | last post by:
Hello Everybody, I am writing an interactive tutorial for JavaScript. I created a text box into which the users can input javascript commands. On pressing a button, these commands will be...
15
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
156
by: jacob navia | last post by:
There is a C tutorial at http://www.cs.virginia.edu/~lcc-win32 It is written to go with the compiler, available at the same URL. I have added quite a bit of material, and I would be glad if...
11
by: Magnus Lycka | last post by:
While the official Python Tutorial has served its purpose well, keeping it up to date is hardly anyones top priority, and there are others who passionately create really good Python tutorials on...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
17
Banfa
by: Banfa | last post by:
I felt that this was a good point to start a tutorial on C/C++ programming because clearly we need to have some idea of what we are trying to achieve before we start out. I recently found this...
2
by: TonyJ | last post by:
Hello! I use VS2005 and C#. I'm looking at ADO.NET and have found some test tutorial on microsoft MSDN. It can be found on google when searching for "Walkthrough: Saving Data to a Database...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
by: Omar Abid | last post by:
Reason of this project: The Microsoft.VisualBasic.Interaction class exposes many useful commands and methods that were available in Visual Basic like AppActivate, Beep, Callbyname... This...
182
by: Bill Cunningham | last post by:
I understand this code. int a; int b; for (b=0;b<5;b=b+1) int a; This should take every element of the array a and set it to 1,2,3,4,5. Great. Now for the big question. How would you work...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.