Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Beginner in Functions (python)

Options
  • 01-04-2013 5:10pm
    #1
    Registered Users Posts: 2,128 ✭✭✭


    Hey guys,

    learning python on a on-line tutorial. this example doesn't seem to work (just keeps looping back the initial menu).

    can someone advise?

    thank you
    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: ")
    print (menu())
       
    # 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!")
    


Comments

  • Registered Users Posts: 339 ✭✭duffman85


    you're nearly have it...

    read up on the input function in the python documentation http://docs.python.org/3.3/library/functions.html ;)
    For future reference surround your code with
    tags, especially with a language like python where the spaces/tabs are important.
    
    Here's your code reformatted:
    [code]
    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: ")
    print (menu())
    
    # 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!")
    


  • Registered Users Posts: 2,128 ✭✭✭thorbarry


    Nice one, thanks alot dude. I'll give it a crack when I'm home :D


  • Registered Users Posts: 2,128 ✭✭✭thorbarry


    hey tried that and still getting the same issue :(

    can you or anyone advise?


  • Registered Users Posts: 339 ✭✭duffman85


    http://docs.python.org/3.3/library/functions.html#input

    input returns a string not an integer. to see this open a python shell
    >>> test=input("Enter a number: ")
    Enter a number: 5
    >>> test
    '5'
    >>> if test==5:
    	print("test equals five")
    else:
    	print("test does not equal five")
    	print(test.__class__)
    
    	
    test does not equal five
    <class 'str'> 
    
    A string will never equal a number - so you need to convert it to an integer(int) using the int(x) function

    So you could do
    return int(input ("Choose your option: "))
    


Advertisement