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))
def gcd(a, b): rem = 1 while rem != 0: b, rem = divide(a, b) a = b b = rem return a
def divide_by_zero(self): a = 2 b = 0 result = division.divide(a, b) self.assertEqual(result, "Invalid")
def test_do_divide(self): first_arg = 4 second_arg = 2 result = division.divide(first_arg, second_arg) expected_result = 2 self.assertEqual(result, expected_result)
import multiplication import addition import division import mod import subtraction x = 8 y = 5 result_of_subtraction = subtraction.sub(x, y) result_of_addition = addition.add(x, y) result_of_multiplication = multiplication.multiply(x, y) result_of_division = division.divide(x, y) result_of_mod = mod.mod(x, y) print("X=8 and Y=5") print("Result of subtraction: " + str(result_of_subtraction)) print("Result of addition: " + str(result_of_addition)) print("Result of multiplication: " + str(result_of_multiplication)) print("Result of division: " + str(result_of_division)) print("Result of mod: " + str(result_of_mod))
b = float(input("Enter second Number: ")) if answer == '1': # ADDITION FUNCTION result_add = add(a, b) print(f'\nResult of addition of {a} and {b} = {result_add} \n') elif answer == '2': # SUBTRACTION FUNCTION result_subtract = subtract(a, b) print( f'\nResult of subtraction of {a} and {b} = {result_subtract} \n') elif answer == '3': # MULTIPLICATION FUNCTION result_multiply = multiply(a, b) print( f'\nResult of multiplication of {a} and {b} = {result_multiply} \n' ) elif answer == '4': # DIVISION FUNCTION result_divide = divide(a, b) print(f'\nResult of division of {a} and {b} = {result_divide} \n') elif answer == '5': # POWER FUNCTION result_power = power(a, b) print(f'\nResult of {a} to the power of {b} = {result_power} \n') else: # IF INPUT ISN'T 1,2,3,4,5 IT'LL BE TERMED AS INVALID print("\nInvalid Input try again \n") # add_result = add(a, b) # print(f'Result of addition of {a} and {b} = {add_result}')
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
def divide(self, x1,x2): return dv.divide(x1,x2)
def test_returns_string_infinity_if_divisor_is_zero(self): result = division.divide(20, 0) self.assertEqual("Infinity", result)
from Add import add from Subtraction import subtract #addition - Ayush #subtraction- Keerthana #multiplication - SOmanshu #division - Arjun print("Guide for choosing operations--------------------") print("1-- Addition") print("2-- subtraction") print("3-- multiplication") print("4-- division") ch=int(input('Enter choice: ')) a=int(input("first number: ")) b=int(input("second number: ")) if(ch==1): print(add(a,b)) elif(ch==2): print(subtract(a,b)) elif(ch==3): print(multiply(a,b)) elif(ch==4): print(divide(a,b)) else: print("PLease enter a valid Choice")
def test_floatDivision(self): self.assertEqual(division.divide(1.5, 0.23), 6.521739130434782)
def test_simpleDivision(self): self.assertEqual(division.divide(500, 10), 50.0)
def test_negative_division(self): self.assertEquals(divide(-9, -3), 3)
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)
def test_division(self): self.assertEquals(divide(4, 2), 2)
def test_for_string(self): self.assertEqual(divide("one", 2), "Has to be an integer")
def test_float_division(self): self.assertEquals(divide(6.25, 2.5), 2.5)
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")
from multiplication import multiply from division import divide from register import * a = int(input()) b = int(input()) r_a = store_in_register(a) r_b = store_in_register(b) if DEBUG: print(r_a, r_b) binary_product = multiply(r_a, r_b) binary_quotient, binary_remainder = divide(r_a, r_b) decimal_product = convert_decimal(binary_product) decimal_quotient = convert_decimal(binary_quotient) decimal_remainder = convert_decimal(binary_remainder) print("decimal product:", decimal_product) print("decimal_quotient:", decimal_quotient) print("decimal_remainder:", decimal_remainder) print("binary_product:", binary_product) print("binary_quotient", binary_quotient) print("binary_remainder", binary_remainder) assert (decimal_product == convert_to_decimal(binary_product)) assert (decimal_quotient == convert_to_decimal(binary_quotient)) assert (decimal_remainder == convert_to_decimal(binary_remainder))
def test_overflow(self): with self.assertRaises(OverflowError): division.divide(1e100, 1)
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("")
def test_negativeDivision(self): self.assertEqual(division.divide(-400, 20), -20.0)
def test_divides_numbers(self): result = division.divide(20, 5) self.assertEqual(4, result)
def test_divisionByZero(self): with self.assertRaises(ValueError): division.divide(5, 0)
def test_divide_when_other_number_is_zero_raises_an_exception(): with pytest.raises(ZeroDivisionError, match="division by zero"): divide(2, 0)