def division(): num1 = request.args.get('num1') num2 = request.args.get('num2') if num1 is None: num1 = 0 if num2 is None: num2 = 0 return render_template('division.html', num1=num1, num2=num2, division=functions.division(float(num1), float(num2)))
def main(): assert test_int([1, 2, 3, 4]) == True assert test_int([1, 2, 'e', 3]) == False assert length([1, 2, 3]) == 3 assert sort_list([3, 2, 1]) == [1, 2, 3] assert find_max([1, 2, 3]) == 3 assert find_max([1, 1, 1]) == 1 assert find_min([1, 2, 3]) == 1 assert find_num([1, 2, 3], 2) == 1 assert square_all([1, 2, 3]) == [1, 4, 9] assert check_len([1, 2, 3], [2, 3]) == 'Two lists have different length.' assert check_len([1, 2, 3], [1, 2, 3]) == 'Two lists have same length.' assert summation([1, 2, 3], [2, 3]) == [3, 5, 3] assert multiplication([1, 1, 1], [2, 2, 2]) == [2.0, 2.0, 2.0] assert division([2, 2, 2], [1, 1, 1]) == [2, 2, 2] assert subtraction([2, 2, 2], [1, 1, 1]) == [1, 1, 1] assert sum_atIndex([1, 1, 1], 1, 0) == [2, 1, 1] assert multiplication_atIndex([1, 1, 1], 2, 0) == [2, 1, 1] assert division_atIndex([2, 1, 1], 2, 0) == [1.0, 1, 1] assert subtraction_atIndex([2, 1, 1], 1, 0) == [1, 1, 1] assert concatenation([1, 2, 3], [4, 5, 6]) == [1, 2, 3, 4, 5, 6] assert concatenation_int([1, 2, 3], 4) == [1, 2, 3, 4]
from functions import addition, school_grading, triangle_area, division, large_equal large_equal() addition() school_grading() triangle_area() division()
def test_division(): assert functions.division(1, 1) == 1 assert functions.division(1, 1) != 2
def newClient(self, connection, client_address, port): print(timePrintout() + " New Client: thread created") try: print(timePrintout() + " Connection from", client_address, ":", port) # Receive the data in small chunks and retransmit it while True: data = connection.recv(4096) if len(data) != 0: message = Segment.unpack(data) #message.print() print(message.time() + ' Received: ' + message.printout()) token = message.id numberA = int(message.numberA) numberB = int(message.numberB) if message.operation == OPERATION.id: token = random.randint(100, 1000) connection.sendall( Segment(OPERATION.id, True, token, time.time(), "", "", "").pack()) elif message.operation == OPERATION.addition: result = functions.addition(numberA, numberB) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.addition, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.addition, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.subtraction: result = functions.subtraction(numberA, numberB) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.subtraction, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.subtraction, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.multiplication: result = functions.multiplication(numberA, numberB) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.multiplication, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.multiplication, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.division: result = functions.division(numberA, numberB) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.division, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.division, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.exponentiation: result = functions.exponentiation(numberA, numberB) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.exponentiation, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.exponentiation, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.negation: result = functions.negation(numberA) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.negation, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.negation, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.root: result = functions.root(numberA, numberB) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.root, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.root, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.combination: result = functions.combination(numberA, numberB) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.combination, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.combination, True, token, time.time(), 0, 0, result).pack()) elif message.operation == OPERATION.factorial: result = functions.factorial(numberA) if result > 2147483647 or result < -2147483648: connection.sendall( Segment(OPERATION.factorial, False, token, time.time(), 0, 0, 0).pack()) else: connection.sendall( Segment(OPERATION.factorial, True, token, time.time(), 0, 0, result).pack()) else: print(timePrintout() + " Error occured: Wizard must be stopped!") # break finally: # Clean up the connection connection.close()
def have_a_chat(): """Main function to run my chatbot.""" chat = True while chat: # Get a message from the user msg = input('INPUT :\t') out_msg = None msg = fu.intList(msg) # Prepare the input message isintList = test_int(msg) # Check for an end msg if fu.end_chat(msg): out_msg = 'Bye!' chat = False if not out_msg and isintList: firstintList = msg.copy() out_msg = USAGE if not out_msg: # Initialize to collect a list of possible outputs outs = [] # Check if the input looks like a greeting, add a greeting output if so outs.append(fu.selector(msg, GREETINGS_IN, GREETINGS_OUT)) #Different input cases to handle output. #Get length if msg == ['l']: outs.append(LS_FMT_1.format(length(firstintList))) #Sort list elif msg == ['s']: firstintList = sort_list(firstintList) outs.append(LS_FMT_2.format(firstintList)) #Get maximum elif msg == ['max']: maximum = find_max(firstintList) outs.append( LS_FMT_3.format(maximum, find_num(firstintList, maximum))) #Get minimum elif msg == ['min']: minimum = find_min(firstintList) outs.append( LS_FMT_4.format(minimum, find_num(firstintList, minimum))) #Return index of certain integer elif msg == ['num']: print('OUTPUT:', findnumPrompt) user_input = input() try: toFind = int(user_input) index = find_num(firstintList, toFind) outs.append(LS_FMT_5.format(index)) except ValueError: outs.append(reType) #Square all integers in current list elif msg == ['sq']: temp_list = square_all(firstintList) outs.append(LS_FMT_6.format(temp_list)) #Show current list elif msg == ['curr']: outs.append(LS_FMT_7.format(firstintList)) #check whether two lists have different length elif msg == ['check']: print('OUTPUT:', Prompt) user_input = input() intList = fu.intList(user_input) if test_int(intList): outs.append(check_len(firstintList, intList)) else: outs.append(reEnter) #sum two lists up elif msg == ['sum']: print('OUTPUT:', Prompt) user_input = input() intList = fu.intList(user_input) #To avoid change to original list. temp_list = firstintList.copy() if test_int(intList): outs.append(LS_FMT_8.format(summation(temp_list, intList))) else: outs.append(reEnter) #Multiply two lists elif msg == ['time']: print('OUTPUT:', Prompt) user_input = input() intList = fu.intList(user_input) temp_list = firstintList.copy() if test_int(intList): outs.append( LS_FMT_8.format(multiplication(temp_list, intList))) else: outs.append(reEnter) #Divide Two lists elif msg == ['divide']: print('OUTPUT:', Prompt) user_input = input() intList = fu.intList(user_input) temp_list = firstintList.copy() if test_int(intList): outs.append(LS_FMT_8.format(division(temp_list, intList))) else: outs.append(reEnter) #Subtract a list from another elif msg == ['minus']: print('OUTPUT:', Prompt) user_input = input() intList = fu.intList(user_input) temp_list = firstintList.copy() if test_int(intList): outs.append( LS_FMT_8.format(subtraction(temp_list, intList))) else: outs.append(reEnter) #Add an integer at certain index elif msg == ['sumindex']: print('OUTPUT:', intPrompt) user_input = input() temp_list = firstintList.copy() try: value = int(user_input) print('OUTPUT:', indexPrompt) index = input() index = int(index) output_list = sum_atIndex(temp_list, value, index) outs.append(LS_FMT_8.format(output_list)) except ValueError: outs.append(reType) #Minus an integer at certain index elif msg == ['multiindex']: print('OUTPUT:', intPrompt) user_input = input() temp_list = firstintList.copy() try: value = int(user_input) print('OUTPUT:', indexPrompt) index = input() index = int(index) output_list = multiplication_atIndex( temp_list, value, index) outs.append(LS_FMT_8.format(output_list)) except ValueError: outs.append(reType) #Divide an integer at certain index elif msg == ['divideindex']: print('OUTPUT:', intPrompt) user_input = input() temp_list = firstintList.copy() try: value = int(user_input) print('OUTPUT:', indexPrompt) index = input() index = int(index) output_list = division_atIndex(temp_list, value, index) outs.append(LS_FMT_8.format(output_list)) except ValueError: outs.append(reType) #Subtract an integer at certain index elif msg == ['minusindex']: print('OUTPUT:', intPrompt) user_input = input() temp_list = firstintList.copy() try: value = int(user_input) print('OUTPUT:', indexPrompt) index = input() index = int(index) output_list = subtraction_atIndex(temp_list, value, index) outs.append(LS_FMT_8.format(output_list)) except ValueError: outs.append(reType) #Concat two lists together elif msg == ['concat']: print('OUTPUT:', Prompt) user_input = input() intList = fu.intList(user_input) if test_int(intList): outs.append( LS_FMT_8.format(concatenation(firstintList, intList))) else: outs.append(reEnter) #Concat an integer to end of current list elif msg == ['concatkey']: print('OUTPUT:', intPrompt) user_input = input() temp_list = firstintList.copy() try: value = int(user_input) output_list = concatenation_int(temp_list, value) outs.append(LS_FMT_8.format(output_list)) except ValueError: outs.append(reType) #Unknown cases else: outs.append(USAGE + Unknown) options = list(filter(None, outs)) if options: out_msg = options[0] #Print to output print('OUTPUT:', out_msg)
def test_division(self): self.assertEqual(division(5, 1), 5) self.assertEqual( division(4, 0), None ) # There is no result in dividing 4 by 0, hence comparing to none. See func definition self.assertEqual(division(5, 2), 2.5)
def test_division(self): self.assertRaises(ZeroDivisionError, division, 5, 0) self.assertEqual(division(6, 3), 2)
def do_div(self, arg): """Usage: div <numberA> <numberB>""" print(division(arg['<numberA>'], arg['<numberB>']))