Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please, the code should be in a text file in the format and have to be 2 codes ( code.py and Mylib.py) with (##### comments......).

Please, the code should be in a text file in the format and have to be 2 codes ( code.py and Mylib.py) with (##### comments......). Please, test your code before sending it.

Do not use print, replace it with return......

Do not include UI functions in the def function library.

You are going to enhance the prior assignment by doing the following:

1) Move all the functions into Mylib.py

2) Use import to include Mylib into the code

3) Test the code and make sure that the prior code is still working

4) Add the following function into Mylib

scalc(p1)

p1 will be a string like this "N1, N2, operator"

examples

scalc("20,30,*")

the result will be 600

scalc("50,20,+")

the result will be 70

scalc("50,20,-")

the result will be 30

scalc("60,20,/")

the result will be 30

use string functions to parse the first number, the second number, and the operator from the input string.

use the prior functions (add, subtract, divide, and multiply ) to do the calculations.

The function scalc will be get called after user entered "N" for the "Continue Looping Y/N" question.

Sample output

Enter your Lower range ---> 10

Enter your Higher range ---> 20

Enter N1,N2,operator ---> 15

Enter your Second number ---> 17

The Result of 15.0+17.0=32.0

The Result of 15.0-17.0=-2.0

The Result of 15.0*17.0=255.0

The Result of 15.0/17.0=0.882352941176

Continue Looping Y/N Y

Enter your Lower range ---> 20

Enter your Higher range ---> 30

Enter your First number ---> 25

Enter your Second number ---> 50

The input values are outside the input ranges

Please check the numbers and try again

Continue Looping Y/NN

Enter N1,N2,operator ---> 15,17,*

The Result of 15.0*17.0=255.0

Thanks for using our calculator

This is prior code:

def add(n1, n2):

return n1 + n2

def sub(n1, n2):

return n1 - n2

def mult(n1, n2):

return n1 * n2

def div(n1, n2):

return n1 / n2

def IsinRange(lr, hr, n):

return lr <= n <= hr

def read_float(prompt):

while True:

# Here is one exception handling

# if the input is not a valid number, then exception will be caught, and prompt to re-enter a number

try:

return float(input(prompt))

except ValueError:

print("Invalid input. Try again!")

def main():

choice = 'y'

while choice == 'y' or choice == 'Y':

low = read_float("Enter your Lower range ---> ")

high = read_float("Enter your Higher range ---> ")

first = read_float("Enter your First number ---> ")

second = read_float("Enter your Second number ---> ")

if IsinRange(low, high, first) and IsinRange(low, high, second):

print("The Result of {}+{}={}".format(first, second, add(first, second)))

print("The Result of {}-{}={}".format(first, second, sub(first, second)))

print("The Result of {}*{}={}".format(first, second, mult(first, second)))

# Here is one exception handling

# if number is divided by 0, exception will be thrown

# catch the exception and print a message

try:

print("The Result of {}/{}={}".format(first, second, div(first, second)))

except ZeroDivisionError:

print("Can't divide a number by 0")

else:

print("The input values are outside the input ranges")

print("Please check the numbers and try again")

print("Thanks for using our calculator")

break

choice = input("Continue Looping Y/N ")

print()

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions