[Tutor] Return to main menu

Brian van den Broek bvande at po-box.mcgill.ca
Tue Jun 29 02:38:16 EDT 2004


K J said unto the world upon 29/06/2004 01:51:

K J said unto the world upon 29/06/2004 01:51:

> Well this is the little program that I have created it is my first one
> and it works just fine from what I have tested. Only problem I have
> with it is that, lets say you enter 1 at the prompt it will load the
> add.py modual. however when it comes to the end of the modual it
> terminats. How would I go about making it so that it will return to the
> menu and ask for another choice.
> 
> Kevin
> 
> print """
> 0 Exit
> 1 Add
> 2 Subtract
> 
> """
> number = raw_input("Enter a number: ")
> 
> while 1:
>         try:
>             number = int(number)
>             break
>         except ValueError:
>             print "That is not an option"
>             number = raw_input("Enter a number: ")
> if int(number) == 1:
>         import add
> if int(number) == 2:
>         import subtract
> 
> 

Hi Keven,

A few things strike this relative newbie:

1) You probably intend the "if int(number) ==" blocks to be nested into
the while loop. (I assume you want to do something with the first choice
before asking for another one.)

2) If I am right in thinking that you will want to nest the conditional
blocks, you will have to do something about the imports. (You can only
import a module once per program run; afterwards you have to reload it.)

3) More importantly, it breaks out of the loop because you told it to ;-)
(The best thing about computers is they do exactly what you tell them to.
The worst thing about computers is they do exactly what you tell them to.)
Try replacing the "break" with "continue"; that will keep you in the loop,
but not keep asking the user for input. It won't hit the imports either if 
you indent them. And even if you don't, it won't be having any real 
effect, as the loop would then continue on its own, anyway. So, better 
still, remove it entirely.

3) To reprint the menu, put your menu-printing code into a function
definition and call that just before number = int(number). I'd also put 
the "is the input an integer" testing logic into a function.

4) Then, you'd have a while loop which asked for input via your menu and 
input function, tested it for being an integer and re-requested if not, 
reloaded the modules as requested and then after they finished went back 
to the top of the while loop and called the menu function again.

5) If I am right in thinking that you will want to nest the conditional
blocks, you will have to do something about the imports. (You can only
import a module once per program run; afterwards you have to reload it.)


Functions are your friends :-) You typed "number = raw_input("Enter a 
number: ")" twice. Try this at the start of your code instead:

def ask():
     input = raw_input("Enter a number: ")
     return input

now you can just type:

number = ask()

Expand that function to run the test on the input too, and you are on your 
way.

And I see Orri beat me to posting; so this might be redundant in places. 
Oh well.

Best to all,

Brian vdB




More information about the Tutor mailing list