FudgeCoder 0 Newbie Poster

Here is my code for a calculator. What I don't understand is how it knows which function to execute when someone chooses an option, how does it know to add and not subtract? The program works perfectly. I'm just looking for an explanation on how it knows if I press 1 to add.

#calculator program v 2.0
#main menu
def menu():
    #print options
    print " "
    print "Your options: "

    print "1. Addition"
    print "2. Subtraction"
    print "3. Multiplication"
    print "4. Division"
    print "5. Power to"
    print "6. Quit"
    print " "
    return input ("Choose your option: ")

#addition
def add(a,b):
    print a, "+", b, "=", a+b

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

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

#division
def div(a,b):
    print a, "/", b, "=", a/b

#power to
def pow(a,b):
    print a, "^", b, "=", a**b

#Code loop
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:
        pow(input("Take this: "),input("and put it to this: "))
    elif choice==6:
        loop=0
        print "Thank you for using calculator.py!"
    else:
        print"I'm sorry please try that again."
#program ends
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.