In: Computer Science
This lab problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules.
We have given you some incomplete code; note that the very first line of that code contains an import statement as follows:
import math
This statement enables your program to use a math module. A module is a collection of instructions saved together as a whole. So, in this lab you are using the math module. Any time we choose to use a module, we must use an import statement to bring this module into the program. When we import the math module, all the computer instructions contained in the math module are made available to our program, including any functions that have been defined.
If you are interested in finding out what is in the math module you can always go to the Python docs and take a look: Python Docs
Example of using the math module: print(math.floor(5.4)) will use the floor() function as defined in the math module, and in this case with the argument 5.4, will result in printing the value 5.
Below is the definition of your problem that we are asking you to solve using the math module.
Prompt the user for floating point numbers x, y and z. For the x value your you will the use the following prompt Enter x value:inside of your input statement. Using the same approach you will prompt the user for y and z.
- Given three floating-point numbers x, y, and z, your job is to output - the **square root** of x, - the **absolute value** of (y minus z) , and - the **factorial** of (the **ceiling** of z).
Example of a sample run:
Enter x value:5 Enter y value:6.5 Enter z value:3.2
Then the expected output from these entries is:
2.23606797749979 3.3 24
Hint: For finding out the square root you will need to use math.sqrt(), you need to use the website recommended above to find out which functions to use in order to solve the rest of the lab problem.
# import the math module
import math
#input the values of x,y and z by casting string to float
x = float(input("Enter X Value"))
y = float(input("Enter Y Value"))
z = float(input("Enter Z Value"))
f=1 #set the value of f to 1 for finding the factorial
# print the square root of x
print("Square root of",x," is ",math.sqrt(x))
#print the absolute value of y-z
print("Absolute value of ",y,"-",z," is ",abs(y-z))
#find the factorial
for i in range(1,math.ceil(z)+1): #ceil function is used here to
round up the value of z
f = f * i #multiply the values with 1
#display the factorial
print("Factorial of ",math.ceil(z)," is ",f)
PROGRAM IN JPEG FORMAT
OUTPUT
SAME LINE OUTPUT
output