Question

In: Computer Science

A2_1 Function and modular a) Write a module fun_math.py that contains three functions: cal_factorial(x) – receives...

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.

Solutions

Expert Solution

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...


Related Solutions

Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Modules Assignment pythonWrite a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.Write a separate Python program (in a separate file from you Python module) that imports your above module and uses several of its functions in your program. Make use of the input() and print() functions in this program file. Try to make your program as a real-world application.
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt the user to enter the sales amount and computes and prints with a description the commission paid to salesperson as follows: 10% for sales amount less than $2,000.00, 15% for sales amount less than $10,000.00 and 20% for sales amount less than $20,000.00, then function calc_commission calls another function name assign_base_salary() to ask the user to enter each of 5 salesperson’s base salary ,...
Create and submit a Python program (in a module) that contains a main function that prints...
Create and submit a Python program (in a module) that contains a main function that prints 17 lines of the following text containing your name: Welcome to third week of classes at College, <your name here>! can someone please show me how to do this step by step? I'm just confused and keep getting errors in idle.
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2)...
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2) t is a turtle object passed in coord1 and coord2 are 2-element lists that represent x, y coordinates draws a line from coord1 to cord2 def poly(t, *coords) t is a turtle object passed in *coords is any number of x, y coordinates each as a 2-element list draws a polygon based on coords (will close off polygon by drawing a line from last...
Just need the desktop test for this two function a) Write a function that receives the...
Just need the desktop test for this two function a) Write a function that receives the number of terms (n) and returns a container with the first n TunaPoke numbers. Present a "print screen" of the results and their respective desktop test when the function is invoked with the 16. (b) Write a function that receives a limit number and returns a container with the first numbers TunaPoke that do not exceed the specified limit. Present a "print screen" of...
Write the demand functions for the following utility function: U = ln(x) + ln(y)
Write the demand functions for the following utility function: U = ln(x) + ln(y)
PYTHON: Complete these functions in the module: f2c, a function to convert Fahrenheit temperatures to Celcius....
PYTHON: Complete these functions in the module: f2c, a function to convert Fahrenheit temperatures to Celcius. c2f, a function to convert Celcius termperatures to Fahrenheit. Write f2c() and c2f() as functions that return a true value or False. In other words, these functions do not print — they return. A "true value", in this case, would be a number that corresponds to a temperature. If the user enters an invalid temperature, the function should return False. Create _main(), a function...
Write three functions that compute the square root of an argument x using three different methods....
Write three functions that compute the square root of an argument x using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number x is the value s such that s*s == x. For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that x is in the range 0.0 to 100.0. You program should have a main() that asks the user...
PYTHON: Write a script that imports the functions in the module below and uses all of...
PYTHON: Write a script that imports the functions in the module below and uses all of the functions. import math def _main():     print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")     f = -200     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     f = 125     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     c = 0     print("{:.2f} C is {:.2f} F".format(c, c2f(c)))     c = -200     print("{:.2f} C is {:.2f} F".format(c, c2f(c))) def f2c(temp):     #Converts Fahrenheit tempature to Celcius     if temp <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT