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 multiply_and_send_result(number1, number2): result = multiplication.multiply(number1, number2) callback_url = '{url}?number={number}'.format(url=CALLBACK_URL, number=result) response = requests.get(callback_url) if not response: logger.error( 'Multiplication result could not be sent to given callback.') return False return True
def calculate(a): return multiply(a, a)
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 multiply(x, y): return x * y def divide(x, y): return x / y #take input from user print("Welcome to simple calculator, please select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice 1/2/3/4:") num1 = int(input("Enter first number: ")) num2 = int(input("Enter the second number:")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "x", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) else: print("Invalid input")
def divide_simple(dividend, divisor): len_dividend = len(dividend) len_divisor = len(divisor) # 1. 生成除数的倍数表(1-9) len_substractor = 9 tbl_substractor = [] tbl_substractor.append(divisor) for i in xrange(2,len_substractor+1): num, _, power_ten = multiply(divisor, i) tbl_substractor.append(num + [0] * power_ten) quotient = [] remainder = dividend[:len_divisor-1] len_remainder = len_divisor -1 # 2. 将除法换算成阶梯型减法 for i in xrange(len_divisor-1, len_dividend): if len_remainder == 0 and dividend[i] == 0: if len(quotient) > 0: quotient.append(0) continue remainder.append(dividend[i]) subtractor_idx = None min_idx = 0 max_idx = len_substractor-1 if not is_smaller_or_equal(tbl_substractor[min_idx], remainder): if len(quotient) > 0: quotient.append(0) continue if is_smaller_or_equal(tbl_substractor[max_idx], remainder): subtractor_idx = max_idx else: # 二分法查找:比remainder小的最大substractor while min_idx<=max_idx: mid_idx = (min_idx + max_idx) / 2 if is_smaller_or_equal(tbl_substractor[mid_idx], remainder): min_idx = mid_idx+1 subtractor_idx = mid_idx else: max_idx = mid_idx-1 # if not is_smaller_or_equal(divisor, remainder): # if len(quotient) > 0: # quotient.append(0) # continue # # for i in xrange(len_substractor): # if is_smaller_or_equal(tbl_substractor[-1-i], remainder): # subtractor_idx = -1-i+len_substractor # break if subtractor_idx is None: continue digit_quotient = subtractor_idx+1 quotient.append(digit_quotient) subtractor = tbl_substractor[subtractor_idx] for i, digit_substractor in enumerate(subtractor[::-1]): digit_remainder = remainder[-1 - i] # 从右往左按位做减法,若不够减向高位借1 if digit_remainder < digit_substractor: remainder[-2 - i] -= 1 # 左边是高位 remainder[-1 - i] = digit_remainder + 10 - digit_substractor else: remainder[-1 - i] = digit_remainder - digit_substractor # 高位的0去掉 remainder = list_strip_zero(remainder) return quotient, remainder
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))
print("This is the main file") import addition import Division import multiplication import subtraction import modulus op = input( "Select operation you wish to perform:\n 1.Addtion\n 2.Subraction\n 3. Multiplication\n 4. Division\n 5.Modulus\n" ) a = input("Enter the first number: ") b = input("Enter the second number: ") if (op == 1): print(addition.add(a, b)) elif (op == 2): print(subtraction.sub(a, b)) elif (op == 3): print(multiplication.multiply(a, b)) elif (op == 4): print(Division.div(a, b)) elif (op == 5): print(modulus.mod(a, b))
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("")
import add, subtract, multiplication, divide, modulus print( "<-- Mathematical Program -->\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Modulus" ) inp = int(input("Enter your choice->")) if (inp == 1): print(add.add()) elif (inp == 2): print(subtract.subtract()) elif (inp == 3): print(multiplication.multiply()) elif (inp == 4): print(divide.div()) elif (inp == 5): print(modulus.mod()) else: print("Invalid choice")
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_multiplication(self): self.assertEqual(multiplication.multiply(2, 2), 4) self.assertEqual(multiplication.multiply(10, 2), 20)