## Beginner in Functions

This is the place for queries that don't fit in any of the other categories.

### Beginner in Functions

Hey Guys,

i am learning python and am going through tutorials. I am on functions and the below example is stuck in a loop asking me for the option. can you help or suggest what i am doing wrong?

thank you!

Code: Select all
`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 givendef add(a,b):    print (a, "+", b, "=", a + b)    # this subtracts two numbers givendef sub(a,b):    print (b, "-", a, "=", b - a)    # this multiplies two numbers givendef mul(a,b):    print (a, "*", b, "=", a * b)    # this divides two numbers givendef div(a,b):    print (a, "/", b, "=", a / b)    # NOW THE PROGRAM REALLY STARTS, AS CODE IS RUNloop = 1choice = 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 = 0print ("Thankyou for using calculator.py!")`
Last edited by glenmoe85 on Tue Apr 02, 2013 8:51 pm, edited 1 time in total.
glenmoe85

Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm

### Re: Beginner in Functions

Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Beginner in Functions

can anyone help me with this issue. I have googled the hell out of this and i know the solution is obvious but at the moment i dont see it (from looking at it to much).

thankyou!
glenmoe85

Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm

### Re: Beginner in Functions

Yoriz was refering to the fact that your code is not in code tags.

I mean i could guess your code's indentation, but i am not going to.
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Beginner in Functions

Code: Select all
`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 givendef add(a,b):   print (a, "+", b, "=", a + b)# this subtracts two numbers givendef sub(a,b):   print (b, "-", a, "=", b - a)# this multiplies two numbers givendef mul(a,b):   print (a, "*", b, "=", a * b)# this divides two numbers givendef div(a,b):   print (a, "/", b, "=", a / b)# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUNloop = 1choice = 0while 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 = 0print ("Thankyou for using calculator.py!")`

what is your problem to be exact?, since that code working good for me
and yes, going by that code, you'll indeed stuck on a loop until you pick 5 to quit the program.

Posts: 12
Joined: Sun Mar 31, 2013 4:48 pm

### Re: Beginner in Functions

Thanks for the replies.

If I pick option 1 - 4, the question repeats. If I pick 5 it does the same, it just repeats the question.
glenmoe85

Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm

### Re: Beginner in Functions

This code seems to be written for python 2.
If you're using python 3, it will not work properly.

The main reason is the difference in the way input() works.
In python 2, it evaluated the input, and it's use was discouraged.
In python 3, it returns the input as string, like raw_input() in python 2.

To convert your input to a number, use int()/float()/whatever, depending on your needs.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Beginner in Functions

that works!!!! thank you so much for your help!
glenmoe85

Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm