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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Beginner in Functions (python)

  • 01-04-2013 4: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