def main(): print("Select Operation") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") print("5.Power") choice = input("Enter Choice(+,-,*,/,^): ") num1 = int(input("Enter first number: ")) num2 = int(input("Enter Second number:")) if choice == '+': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '-': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '*': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '/': print(num1, "/", num2, "=", divide(num1, num2)) elif choice == '^': print(num1, "^", num2, "=", power(num1, num2)) else: print("Invalid input") main()
def ifthen_operate(number_1, number_2): operation = request.args.get('operation') if operation == "addition": return "{}".format(add(number_1, number_2)) elif operation == "subtraction": return "{]".format(subtract(number_1, number_2)) elif operation == "multiplication": return "{}".format(multiply(number_1, number_2)) elif operation == "division": return "{}".format(divide(number_1, number_2)) else: return "Error: need an operation for two numbers. After the url, type 'operation=', then the operation. Operations are: addition, subtraction, multiplication, and division."
def calculate_expression(expression): (x, sign, y) = expression.split(' ') x = float(x) y = int(y) if sign == '+': result = add(x, y) elif sign == '-': result = subtract(x, y) elif sign == '*': result = multiply(x, y) elif sign == '/': result = divide(x, y) elif sign == '^': result = power(x, y) else: raise Exception(f'Invalid sign {sign}') return f'{result:.2f}'
def run(): print("This program will help with simple calculations. What do you want to do?") print("1 - add numbers") print("2 - subtract numbers") print("3 - multiply numbers") print("4 - divide numbers") answer = input(">> ") a = int(input("A=")) b = int(input("B=")) if answer == "1": result = add(a, b) if answer == "2": result = subtract(a, b) if answer == "3": result = multiply(a, b) if answer == "4": result = divide(a, b) print("Result =", result)
def testCode(data): for number in data: if number == 0: print("Skipping 0\n") continue try: print("{0} squared is {1}\n".format(number, operations.squared(number))) print("{0} cubed is {1}\n".format(number, operations.cubed(number))) print("{0} multiplied by 3 is {1}\n".format( number, operations.multiply(number, 3))) print("{0} halved is {1}\n".format(number, operations.halved(number))) print("{0} quartered is {1}\n".format( number, operations.quartered(number))) print("{0} divided by zero is {1}\n".format( number, operations.divide(number, 0))) except ZeroDivisionError: print("Divisor cannot be zero\n")
def test_division(self): assert 1 == operations.divide(10, 10)
import operations print("Welcome to our special calculator where no result is under 0") print("Choose an operation:") print("a) Add") print("b) Subtract") print("c) Multiply") print("d) Divide") user_choice = input("> ") # a, b, c, or d number1 = int(input("Please give me a number: ")) number2 = int(input("Please give me another number: ")) if user_choice == 'a': print(operations.add(number1, number2)) if user_choice == 'b': print(operations.subtract(number1, number2)) if user_choice == 'c': print(operations.multiply(number1, number2)) if user_choice == 'd': print(operations.divide(number1, number2))
def test_divide(): assert divide(1, 2) == 0.5 assert divide(10, 2) == 5
def add(num1, num2): return num1 + num2 def multiply(num1, num2): return num1 * num2 def divide(num1, num2): return num1 / num2 add(2, 3) multiply(5, 9) divide(8, 4) #any number of aguments: def add_any_number_of_arguments(*nums): for num in nums: num += num return num add_any_number_of_arguments(1, 23, 54, 54) #Any number of Keyword/Named arguments:**kwargs '''Similar to *args, we can use **kwargs to pass as many keyword arguments as we want, as long as we use **.'''
def test_divide(self): result = operations.divide(10, 5) self.assertEqual(result, 2)
def test_divide(self): self.assertEqual(operations.divide(10, 5), 2) self.assertEqual(operations.divide(-1, 1), -1) self.assertEqual(operations.divide(-1, -1), 1) self.assertEqual(operations.divide(5, 2), 2.5)
import operations try: a=float(input("enter a")) b=float(input("enter b")) print(operations.add(a,b)) print(operations.subtract(a,b)) print(operations.multi(a,b)) print(operations.divide(a,b)) except: print("it is string")
def test_division_operation_returns_error_when_divided_by_zero(self): self.assertEqual(divide(10, 0), "Zero Division Error!")
def test_division_operation_returns_incorrect_value(self): self.assertEqual(divide(100, 10), 10)
def test3(self): self.assertEqual(operations.divide(18, 2), 9)
result = operations.subtract(operands[0], operands[1]) operands.pop(0) # remove operand 1 operands.pop(0) # remove operand 2 operands.insert( 0, result ) # places the result of operation at the place of two used operands elif x == '*': # Subtraction result = operations.multiply(operands[0], operands[1]) operands.pop(0) # remove operand 1 operands.pop(0) # remove operand 2 operands.insert( 0, result ) # places the result of operation at the place of two used operands elif x == '/': # Subtraction result = operations.divide(operands[0], operands[1]) operands.pop(0) # remove operand 1 operands.pop(0) # remove operand 2 operands.insert( 0, result ) # places the result of operation at the place of two used operands else: print('Error.') break # print('operands_working: {}'.format(operands_working)) # print('operands: {}'.format(operands)) if len(operands) == 1: print('\n{} = {}'.format(equation, operands[0]))