Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Beginner in Functions (python)

  • 01-04-2013 05:10PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 2,128 ✭✭✭thorbarry


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


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


    hey tried that and still getting the same issue :(

    can you or anyone advise?


  • Registered Users, Registered Users 2 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