APP 6

profileLisa ap
05-02.zip

Mylib.py

#add 2 numbers def plus(n1,n2): return n1 + n2 #subtract 2 numbers def subtract(n1, n2): return n1 - n2 #multiply 2 numbers def multiply(n1, n2): return n1 * n2 #division, n2 should not be zero def divide(n1, n2): try: div = n1/(n2 + 0.0) return div except ZeroDivisionError as e: print("The result of %s/%s = %s" % (n1, n2,"You cannot divide by Zero")) except Exception : print("Invalid input" , n1, n2) #expression calculator def scalc(val): try: #the epression is assumed to be comma separated ns = val.split(",") #split it into 3 parts #remove whitespaces in the components n1 = int(ns[0].strip()) n2 = int(ns[1].strip()) op = ns[2].strip() print(op) #check if operator is one of the supported if op == "*": return multiply(n1, n2) elif op == "/": return divide(n1, n2) elif op == "+": return plus(n1, n2) elif op == "-": return subtract(n1, n2) else: print("Unknown operator", op) #in case of invalid input catch the error except Exception : print("Invalid input:" , val)

W5_first_last.py

# Program name : # Student Name : # Course : ENTD220 # Instructor : # Date : 01/24/2020 # Description : Simple Arithmetic # Copy Wrong : This is my work import Mylib def test_scalc(): print("---------Expression calculator------------------") print("Enter the expression with format N1,N2,operator:") print("Supported operator * , /, -, +") print("E.g 5, 4, *)") val = input() res = Mylib.scalc(val) print('The result of "' + val + '" is', res) def IsInRange(lr, hr, n): return lr < n and n < hr def main(): low = int(input("Enter Lower range:")) high = int(input("Enter Higher range:")) n1 = int(input("Enter first number:")) n2 = int(input("Enter second number:")) #n1 and n2 must be in range if IsInRange(low , high, n1) and IsInRange(low , high, n2): add = Mylib.plus(n1 , n2) sub = Mylib.subtract(n1 , n2) mul = Mylib.multiply(n1 , n2) #use print formating, %d = double, %f = float, %s = string print("The result of %d+%d=%d" % (n1, n2, add)) print("The result of %d-%d=%d" % (n1, n2, sub)) print("The result of %d*%d=%d" % (n1, n2, mul)) div = Mylib.divide(n1, n2) print("The result of %d/%d=%f" % (n1, n2, div)) else: print("Errors: input out of range") while True: main() yn = str(input("Continue Looping Y/N:")) if yn == "N": break test_scalc() print("Thanks for using our calculator!")