[Tutor] (no subject)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] (no subject)

Wei Juen Lo
here is my code for a calculator:

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 add(a,b):
    print a, "+", b, "=", a + b
    

def sub(a,b):
    print b, "-", a, "=", b - a
    

def mul(a,b):
    print a, "*", b, "=", a * b
    

def div(a,b):
    print a, "/", b, "=", a / b
    
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

    print "Thank you for using calculator.py!"



main()


Few questions:

why do i have to press enter for it to initialise
why does it print the stuff in menu() again after doing the equation
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

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] (no subject)

Alan Gauld
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/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor