[Tutor] (no subject)

Alan Gauld alan.gauld at btinternet.com
Sat Apr 28 15:40:17 CEST 2012


On 28/04/12 14:08, Wei Juen Lo wrote:

> def menu():
>
> print "Welcome to calculator.py"
> print "your options are:"
> print " "
> print "1) Addition"
> print "2) Subtraction"
> print "3) Multiplication"
> print "4) Division"
> print "5) Quit calculator.py"
> print " "
> return input ("Choose your option: ")
>

> def main():
> loop = 1
> choice = 0
> while loop == 1:
> choice = menu()
> if choice == 1:
> add(input("Add this: "),input("to this: "))
> elif choice == 2:
> sub(input("Subtract this: "),input("from this: "))
> elif choice == 3:
> mul(input("Multiply this: "),input("by this: "))
> elif choice == 4:
> div(input("Divide this: "),input("by this: "))
> elif choice == 5:
> loop = 0

> Few questions:
>
> why do i have to press enter for it to initialise

You don't, it goes straight into the menu loop.
You have to press enter after typing input to tell python you have 
finished entering data. But the initialisation is the two lines before 
the while loop starts and you don't need to press enter for that.

> why does it print the stuff in menu() again after doing the equation

Because the first line inside the while loop is the call to the menu 
function:

 > while loop == 1:
 >     choice = menu()


Every time you finish a calculation, because loop is still 1 the program 
returns to the while and repeats the body of the loop.
Starting with the call to menu() to get the next user selection.

> i am trying to understand classes if anyone has time to make all these
> functions go into a class calculator and have it still work and send me
> the code so i can have an example to refer to that would be great

There is absolutely no point in confusing yourself with
classes if you don't understand the basics like the code above.
Figure out how the basics work first before even beginning to
worry about classes.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list