Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

junglebooboo Group Title

Hi, I am currently making a calculator program in python, and im running into a few bugs at the moment.. first of all, my subtraction and addition functions wont work. secondly I havent found a good way to return to the start, when a user enters wrong values in the enter value section.

  • one year ago
  • one year ago

  • This Question is Closed
  1. junglebooboo Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    def convertstring(str): try: returnValue = int(str) except ValueError: returnValue = float(str) return returnValue def addition( a, b): return converstring(a) + convertstring(b) def subtraction(a, b): return convertstring(a) - convertstring(b) def multiplication(a, b): return convertstring(a) * convertstring(b) def division(a, b): return convertstring(a) / convertstring(b) keepProgramRunning = True print 'Welcome to the Calculator Program!' while keepProgramRunning: print "Please choose what you'd like to do" print "0: Addition" print "1: Subtraction" print "2: Multiplication" print "3: Division" print "4: Quit Program" #Capture the menu choice. choice = int(raw_input()) if choice == 0: try: numberA = raw_input("Enter your first number") numberB = raw_input("Enter your second number") print 'Your result is: ' print addition(numberA, numberB) except NameError: print 'wrong value entered' elif choice == 1: numberA = raw_input("Enter your first number") numberB = raw_input("Enter your second number") print 'Your result is: ' print subtraction(numberA, NumberB) elif choice == 2: numberA = raw_input("Enter your first number") numberB = raw_input("Enter your second number") print 'Your result is: ' print multiplication(numberA, numberB) elif choice == 3: numberA = raw_input("Enter your first number") numberB = raw_input("Enter your second number") print 'Your result is: ' print division(numberA, numberB) elif choice == 4: print 'Thank you for using the calculator program' keepProgramRunning = False else: print 'please choose a valid option' print '/n'

    • one year ago
  2. junglebooboo Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    as you can see, I have made an attempt in the addition part

    • one year ago
  3. carlsmith Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    How about... def convert_string(string): try: return int(string) except ValueError: pass try: return float(string) except ValueError: pass # will return None here automatically def valid_choice(string): try: if int(string) in range(2): # would be range(5) in your case return int(string) except: pass # will return None here automatically def addition(n0, n1): return n0 + n1 def subtraction(n0, n1): return n0 - n1 funcs = [addition, subtraction] print """ 0: Addition 1: Subtraction """ choice = None while choice is None: choice = valid_choice( raw_input("Enter a number from the menu: ") ) n0 = convert_string( raw_input("Enter your first number: ") ) n1 = convert_string( raw_input("Enter your second number: ") ) if n0 is None or n1 is None: print 'Invalid input!!!' else: print "Your result is " print funcs[choice](n0, n1)

    • one year ago
  4. carlsmith Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    That last line calls functions from within a list, and could be written... func = funcs[choice] print func(n0, n1)

    • one year ago
  5. junglebooboo Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    alright, I've moderated the code, but I get stuck in this loop Please choose what you'd like to do 0: Addition 1: Subtraction 2: Multiplication 3: Division 4: Quit Program Please choose what you'd like to do 0: Addition 1: Subtraction 2: Multiplication 3: Division 4: Quit Program

    • one year ago
  6. junglebooboo Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    printing the menu over and over again

    • one year ago
  7. junglebooboo Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    this is the code: def convertstring(str): try: returnValue = int(str) except ValueError: pass try: returnValue = float(str) except ValueError: pass ##Will return none atomatically def valid_choice(str): try: if int(str) in range(5): return int(str) except: pass ##Also returns none def addition( a, b): return converstring(a) + convertstring(b) def subtraction(a, b): return convertstring(a) - convertstring(b) def multiplication(a, b): return convertstring(a) * convertstring(b) def division(a, b): return convertstring(a) / convertstring(b) funcs = [addition, subtraction, multiplication, division] keepProgramRunning = True print 'Welcome to the Calculator Program!' while keepProgramRunning: print "Please choose what you'd like to do" print "0: Addition" print "1: Subtraction" print "2: Multiplication" print "3: Division" print "4: Quit Program" #Capture the menu choice. choice = None while choice is None: choice = valid_choice(raw_input('enter a number from the menu: ')) numberA = raw_input("Enter your first number") numberB = raw_input("Enter your second number") if numberA is None or numberB is none: choice = valid_choice(raw_input('please choose a valid option from the menu')) else: print ' Your result is' print funcs[choice]

    • one year ago
  8. Fertig Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I think it's an indentation error... You have "while keepProgramRunning:" then the menu and the next line doesn't have the indentation anymore

    • one year ago
  9. Fertig Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    *after "# Capture Menu choice"

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.