def calculate(): print('What would you like to do?') print('1. add') print('2. subtract') print('3. multiply') print('4. divide') print() try: operation = int(input('')) except ValueError: operation = int(input('That is not valid input. Please enter 1, 2, 3 or 4: ')) finally: while(operation != 1 and operation != 2 and operation != 3 and operation != 4): #the function would not work properly with the operation = int(input('That is not valid input. Please enter 1, 2, 3 or 4: ')) #while loop nested in an except statement if(operation == 1): print('You chose to add.') result = addition.add() elif(operation == 2): print('You chose to subtract.') result = subtraction.subtract() elif(operation == 3): print('You chose to multiply.') result = multiplication.multiply() elif(operation == 4): print('You chose to divide.') result = division.divide() print() print('The result is ' + str(result))
import subtraction import multiplication import division print("Please make a choice. Select from:") print("1 for add") print("2 for subtract") print("3 for multiply") print("4 for divide") if True: choice = input("Enter selection(1, 2, 3, 4):") if choice in ('1', '2', '3','4'): num1 = float(input("Enter first number:")) num2 = float(input("Enter second number:")) if choice == '1': print(num1, "+", num2, "=", addition.add(num1,num2)) elif choice == '2': print(num1, "-", num2, "=", subtraction.subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiplication.multiply(num1, num2)) elif choice == '4': try: print(num1, "/", num2, "=", division.divide(num1,num2)) except ZeroDivisionError: print("You cannot divide by zero") else: print("Invalid Input")
def test_subtraction(): assert subtract(1, 1) == 1
def main_loop(): print("Welcome to the simple calculator. Please select from the following options:") print() print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") print() user_selection = 0 while True: try: user_selection = int(input("Please enter your selection: ")) print() if (user_selection < 1 or user_selection > 4): raise CalculatorInputError("**Please enter a valid option**\n") else: break except CalculatorInputError as err: print(err) continue except ValueError: print("\n**Please enter an integer**\n") continue while True: try: n1 = int(input("Please enter your first number: ")) print() break except ValueError: # No custom error is necessary here print("\n**Please enter a valid number**\n") continue while True: try: n2 = int(input("Please enter your second number: ")) print() break except ValueError: # No custom error is necessary here print("\n**Please enter a valid number**\n") continue if user_selection == 1: addition.add(n1, n2) elif user_selection == 2: subtraction.subtract(n1, n2) elif user_selection == 3: multiplication.multiply(n1, n2) elif user_selection == 4: division.divide(n1, n2) print() while True: try: response = input("Would you like to perform a new operation (y/n): ") if (response != "y" and response != "n"): raise CalculatorInputError("\n**Please enter a valid option**\n") else: if (response == "y"): print("\n**\n") main_loop() else: quit() except CalculatorInputError as err: print(err) continue
choice = True iteration = 0 while (choice == True): a = input("enter your number: ") while (1): try: a = float(a) break except: print("only numbers allowed") a = input("enter your number: ") if (option == '1'): result = add(a, result) if (option == '2' and iteration == 0): result = subtract(a, result) elif (option == '2'): result = subtract(result, a) if (option == '3'): result = multiply(a, result) if (option == '4' and iteration == 0): result = divide(a, result) elif (option == '4'): result = divide(result, a) iteration = iteration + 1 choice = input( "do you want to add one more number to youe operation y/n : ") == "y" print("your result", result)
import addition import subtraction import multiplication import divide print("Please choose an operation") print("1: Addition") print("2: Subtraction") print("3: Multiplication") print("4: Division") try: userChoice = int(input ("Enter Choice 1 or 2 or 3 or 4 : ")) except: print ('You have entered an invalid option.') else: if userChoice in ('1', '2', '3', '4' ): number1 = int(input("Enter first number: ")) number2 = int(input ("Enter second number: ")) if (userChoice == '1'): print( "Result is: " str(addition.add(number1, number2))) elif (userChoice == '2'): print("Result is: " str(subtraction.subtract(number1, number2))) elif (userChoice == '3'): print ("Result is: " str(multiplication.multiply(number1, number2))) elif (userChoice == '4'): print("Result is: " str(divide.division(number1,number2))) else: print("Invalid user input")
def choose(): while True: print("Welcome to my python calculator. What would you like to do? ") print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") thislist = [] y = True while True: try: user_selection = int(input("My choice is: ")) if user_selection > 0 and user_selection < 5: break else: print("Only integers between 1 and 4 are allowed. Try again") # CalculatorInputError except ValueError: print("Only integers between 1 and 4 are allowed. Try again") while True: n1 = input("Type the number and press enter: ") print("To calculate type '=': ") if n1 == "=": break else: n1 = int(n1) thislist.append(n1) if user_selection == 1: answer = add(thislist) print("answer: " + str(answer)) elif user_selection == 2: answer = subtract(thislist) print("answer: " + str(answer)) elif user_selection == 3: answer = multiply(thislist) print("answer: " + str(answer)) elif user_selection == 4: for x in range(len(thislist) - 1): if thislist[x] == 0: print("cannot divide by zero") print("") choose() answer = divide(thislist) print("answer: " + str(answer)) else: print("Invalid selection. please try again") print("")
from add import add from subtraction import subtract from multiply import multiply from Division import div print("<-- Menu -->\n1.Division\n2.Multiplicatoin\n3.Subtraction\n4.Addition") i = input() if (i == 1): x = input("enter x value") y = input("enter y value") print div(x, y) elif (i == 3): x = input("enter x value") y = input("enter y value") print subtract(x, y) elif (i == 2): x = input("enter x value") y = input("enter y value") print multiply(x, y) elif (i == 4): x = input("enter x value") y = input("enter y value") print add(x, y) else: print("wrong input")