Beginner in Functions

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

Beginner in Functions

Postby glenmoe85 » Mon Apr 01, 2013 4:00 pm

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 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!")
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

Postby Yoriz » Mon Apr 01, 2013 4:14 pm

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
User avatar
Yoriz
 
Posts: 726
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Beginner in Functions

Postby glenmoe85 » Tue Apr 02, 2013 3:25 pm

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

Postby metulburr » Tue Apr 02, 2013 3:40 pm

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.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1331
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Beginner in Functions

Postby siestamadokaist » Tue Apr 02, 2013 8:53 pm

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 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!")

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.
siestamadokaist
 
Posts: 12
Joined: Sun Mar 31, 2013 4:48 pm

Re: Beginner in Functions

Postby glenmoe85 » Tue Apr 02, 2013 9:41 pm

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

Postby stranac » Tue Apr 02, 2013 10:01 pm

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.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Beginner in Functions

Postby glenmoe85 » Wed Apr 03, 2013 8:07 pm

that works!!!! thank you so much for your help!
glenmoe85
 
Posts: 7
Joined: Mon Apr 01, 2013 3:58 pm


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 2 guests