## 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

Hi welcome to the forum please read
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3

Yoriz

Posts: 474
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.
Please use code tags and you will get an answer

I mean i could guess your code's indentation, but i am not going to.

version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr

metulburr

Posts: 856
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!

stranac

Posts: 578
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