In: Computer Science
A2_1 Function and modular
a) Write a module fun_math.py that contains three functions:
cal_factorial(x) – receives a positive integer “x” and returns the factorial of that number.
list_multiples(number, length) – takes a non-negative integer “number” and a positive integer “length”, and returns a list of multiples of “number” up to “length”. For instance (2,3) should return [2,4,6], and (7,5) should return [7,14,21,28,35].
find_max(a_list) – takes a list of integers and returns the largest number.
Note that no built-in functions can be used in this module (len( ), print(), and range( ) are exceptions).
b) Write a program A2.py that contains a main() function and one instruction that calls the main() function in the execution section of the program.
Upon execution, this program prints the following options on the screen:
Please choose your task:
1 – calculate factorial
2 – generate a list of multiples
3 – find max number in a list
Once the user enters their choice of task, the program will prompt a message that asks them to input the argument(s) for each function. For instance, if the user option is 2, the program will print “Please enter a non-negative number and a length: ”. The program then computes the task by calling a function in the fun_math.py module and prints the result on the screen.
Your program should also catch exceptions on unexpected inputs. When an unexpected input is entered, prompt the ask-for-input message again.
A2_2 Testing and tracing
Write the testing code in the fun_math.py module. Provide five testing cases for each function. Debug your code using tracing before you submit.
def cal_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
#-------------------------------------------------------
def list_multiples(number, length):
a = range(length, (length * number)+1, length)
result = list(a)
print(result)
#-------------------------------------------------------
def find_max(a_list):
# Assume first number in list is largest
# initially and assign it to variable "max"
max = a_list[0]
# Now traverse through the list and compare
# each number with "max" value. Whichever is
# largest assign that value to "max'.
for x in a_list:
if x > max :
max = x
# after complete traversing the list
# return the "max" value
return max
a_list = [2,5,4,1,3,10,500,40]
I have uploaded the code of the question above and also I have uploaded the screenshot of the working code with output.
Kindly please see.
Thank You...