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 method(): while True: print("Welcome to the simple calculator in python") op = input("Enter the operator: ") thislist = [] a = True while a == True: n1 = input("enter the number: ") print("To calculate type '=': ") if n1 == "=": a = False else: n1 = float(n1) thislist.append(n1) if op == "+": answer = addition.add(thislist) print("result: " + str(answer)) print(thislist) elif op == "-": answer = subtraction.sub(thislist) print("result: " + str(answer)) elif op == "*": answer = multiplication.mul(thislist) print("result: " + str(answer)) elif op == "/": answer = division.div(thislist) print("result: " + str(answer)) else: print("Invalid operator") print(" ")
def test_basic(self): input_a = [1, 1, 1, 1, 1, 1, 0, 1] input_b = [0, 0, 0, 0, 1, 1, 0, 1] output_number_add = addition.add(input_a, input_b) output_number_imp_add = addition.imp_add(input_a, input_b) expected_number = [1, 0, 0, 0, 0, 1, 0, 1, 0] self.assertEqual(output_number_add, expected_number) self.assertEqual(output_number_imp_add, expected_number)
def buyLotsOfFruit(orderList): """ orderList: List of (fruit, numPounds) tuples Returns cost of order :rtype: integer """ totalCost = 0.0 for item in orderList: totalCost = addition.add(totalCost, fruitPrices[item[0]] * item[1]) return totalCost
def server(): hosted_services = ['add', 'mult'] serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind(("localhost", 5000)) serversocket.listen(5) print "RPC Server Listening" while 1: (clientsocket, address) = serversocket.accept() data = clientsocket.recv(1024) inp = data.split(",") if inp[0] in hosted_services: if inp[0] == "add": clientsocket.send(str(add(int(inp[1]), int(inp[2])))) elif inp[0] == "mult": clientsocket.send(str(mult(int(inp[1]), int(inp[2])))) else: clientsocket.send("Service Not Hosted by RPC Server")
def test_add_2(self): test_param1 = "abcdefg" test_param2 = 20 result = addition.add(test_param1, test_param2) # self.assertTrue(isinstance(result,ValueError)) self.assertIsInstance(result, ValueError)
def test_add(): assert addition.add(2, 3) == 5
choice = input("Enter 1 for addition, 2 for subtraction, 3 for multiplication,4 for division,and 5 for modulo :") a = int(input("enter the first number:")) b = int(input("enter the second number:")) if choice == "1": import addition as a1 a1.add(a, b) if choice == "2": import subtraction as s1 s1.sub(a, b) if choice == "3": import multiplication_function as m1 m1.mul(a, b) if choice == "4": import Division as d1 d1.div(a, b) if choice == "5": import modulus as m2 m2.mod(a, b)
import addition addition.add(10, 20) addition.add(30, 40) import os os.mkdir("Info")
import addition import multiplication import subtract import division print("1: Addition, 2: Subtraction, 3: Multiplication, 4: Division") inp = input("Enter the choice: ") if inp == "1": addition.add() elif inp == "3": multiplication.multiplication() elif inp == "2": subtract.subtraction() elif inp == "4": division.division() else: print("Invalid input")
def main(): addition.add(20, 10) addition.add(30, 70)
from addition import add from subtraction import sub from division import div from multiple import mult print ("Welcome to Calculator Function") print ("Choose option from menu:") print ("1. Add") print ("2. Subtract") print ("3. Multiply") print ("4. Division") choice = int(input("Enter choice (1/2/3/4): ")) num1 = float(input("Enter 1st number: ")) num2 = float(input("Enter 2nd number: ")) if(choice is 1): print (add(num1,num2)) if (choice is 2): print (sub(num1, num2)) if(choice is 3): print (mult(num1,num2)) if (choice is 4): print (div(num1,num2))
#Menu file import addition,subtract,multiplication,division,modulus print("This program is for add,sub,mult and divide\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Modulus") s=int(input("Enter your choice:")) if(s==1): print(addition.add()) elif(s==2): print(subtract.sub()) elif(s==3): print(multiplication.mult()) elif(s==4): print(division.div()) elif(s==5): print(modulus.mod()) else: print("Invalid input")
def test_simple_add(): assert add(1, 2) == 3 assert add(1, 1) == 2 assert add(105, 321) == 426
# Example 021: Python modules import addition import math as math_library result = addition.add(3, 4, 5) print(result) print('Number pi: {}'.format(math_library.pi)) print('Definitions inside addition module: {}'.format(dir(addition)))
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
def main(): a_num = sys.stdin.readline().split(' ') b_num = sys.stdin.readline().split(' ') print addition.add(a_num,b_num) print addition.imp_add(a_num,b_num)
def test_invalid_add(): assert add(-5, 5) is None
def test_add4(self): test_param1 = "" test_param2 = 20 result = addition.add(test_param1, test_param2) self.assertEqual(result, "Please enter numbers.")
from addition import add from subtraction import sub from multiplication import multi from division import div from modulo import mod print ("a=6, b=4") print ("sum:", add(6, 4)) print ("difference:", sub(6, 4)) print ("product:", multi(6, 4)) print ("quotient:", div(6, 4)) print ("remainder:", mod(6, 4))
def test_add5(self): test_param1 = 0 test_param2 = 0 result = addition.add(test_param1, test_param2) self.assertEqual(result, 0)
import addition, message addition.add(10, 20) # 30 addition.add(30, 40) # 70 message.display("Hello World") # Hello World
def test_add_1(self): '''Sample doc string''' test_param1 = 10 test_param2 = 20 result = addition.add(test_param1, test_param2) self.assertEqual(result, 30)
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 addition(self,x1,x2): return ad.add(x1,x2)
def main(): a_num = sys.stdin.readline().split(' ') b_num = sys.stdin.readline().split(' ') print addition.add(a_num, b_num) print addition.imp_add(a_num, b_num)
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))
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")
#import addition #import Add,Multiple import Add, addition, Multiple Add.add(10, 20) addition.add(20, 30) Multiple.mult(2, 3)
import addition for i in range(5): print(addition.add(i, i))
from addition import add from subtraction import sub from division import div from multiple import mult print("Welcome to Calculator Function") print("Choose option from menu:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Division") choice = int(input("Enter choice (1/2/3/4): ")) num1 = float(input("Enter 1st number: ")) num2 = float(input("Enter 2nd number: ")) if (choice is 1): print(add(num1, num2)) if (choice is 2): print(sub(num1, num2)) if (choice is 3): print(mult(num1, num2)) if (choice is 4): print(div(num1, num2))
import addition addition.add(10,20) addition.add(10,50)
import addition addition.add(3, 4)