Monday, June 14, 2010

A Calculator Program in Python using functions and loops

Using a function


#calculator program

#this variable tells the loop whether it should loop or not.
# 1 means loop. anything else means don't loop.

loop = 1

#this variable holds the user's choice in the menu:

choice = 0

while loop == 1:
    #print what options you have
    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 " "

    choice = input("Choose your option: ")
    if choice == 1:
        add1 = input("Add this: ")
        add2 = input("to this: ")
        print add1, "+", add2, "=", add1 + add2
    elif choice == 2:
        sub2 = input("Subtract this: ")
        sub1 = input("from this: ")
        print sub1, "-", sub2, "=", sub1 - sub2
    elif choice == 3:
        mul1 = input("Multiply this: ")
        mul2 = input("with this: ")
        print mul1, "*", mul2, "=", mul1 * mul2
    elif choice == 4:
        div1 = input("Divide this: ")
        div2 = input("by this: ")
        print div1, "/", div2, "=", div1 / div2
    elif choice == 5:
        loop = 0

print "Thank you for using calculator.py!"

Define Your Own Functions



# calculator program

# NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER
# Here we will define our functions
# this prints the main menu, and prompts for a choice
def menu():
    #print what options you have
    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: ")
    
# this adds two numbers given
def add(a,b):
    print a, "+", b, "=", a + b
    
# this subtracts two numbers given
def sub(a,b):
    print b, "-", a, "=", b - a
    
# this multiplies two numbers given
def mul(a,b):
    print a, "*", b, "=", a * b
    
# this divides two numbers given
def div(a,b):
    print a, "/", b, "=", a / b
    
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
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 "Thankyou for using calculator.py!"

1 comment:

  1. # calculator program

    # NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER
    # Here we will define our functions
    # this prints the main menu, and prompts for a choice
    def menu():
    #print what options you have
    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 (" ")


    # this adds two numbers given
    def add(a,b):
    print (a, "+", b, "=", int(a) + int(b))

    # this subtracts two numbers given
    def sub(a,b):
    print (b, "-", a, "=", int(b) - int(a))

    # this multiplies two numbers given
    def mul(a,b):
    print (a, "*", b, "=", int(a) * int(b))

    # this divides two numbers given
    def div(a,b):
    print (a, "/", b, "=", int(a) / int(b))

    # NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
    loop = 1
    menu()
    while loop == 1:

    choice = int(input("choose your option: "))
    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 ("Thankyou for using calculator.py!")




    ##This program initially had lots of bugs .. for eg choice was not defined
    ##as integer , string addition and subtraction was done ... and much more
    ##but now dont worry this rog works well ... thanks to neel shah

    ReplyDelete