Here's the question you clicked on:
junglebooboo
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.
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)
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"