Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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.

Computer Science
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer

SIGN UP FOR FREE
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'
as you can see, I have made an attempt in the addition part
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)

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

That last line calls functions from within a list, and could be written... func = funcs[choice] print func(n0, n1)
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
printing the menu over and over again
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]
I think it's an indentation error... You have "while keepProgramRunning:" then the menu and the next line doesn't have the indentation anymore
*after "# Capture Menu choice"

Not the answer you are looking for?

Search for more explanations.

Ask your own question