A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
 2 years ago
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.
 2 years ago
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.

This Question is Closed

junglebooboo
 2 years ago
Best ResponseYou've already chosen the best response.0def 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'

junglebooboo
 2 years ago
Best ResponseYou've already chosen the best response.0as you can see, I have made an attempt in the addition part

carlsmith
 2 years ago
Best ResponseYou've already chosen the best response.1How 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)

carlsmith
 2 years ago
Best ResponseYou've already chosen the best response.1That last line calls functions from within a list, and could be written... func = funcs[choice] print func(n0, n1)

junglebooboo
 2 years ago
Best ResponseYou've already chosen the best response.0alright, 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

junglebooboo
 2 years ago
Best ResponseYou've already chosen the best response.0printing the menu over and over again

junglebooboo
 2 years ago
Best ResponseYou've already chosen the best response.0this 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]

Fertig
 2 years ago
Best ResponseYou've already chosen the best response.0I think it's an indentation error... You have "while keepProgramRunning:" then the menu and the next line doesn't have the indentation anymore

Fertig
 2 years ago
Best ResponseYou've already chosen the best response.0*after "# Capture Menu choice"
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.