Photo by Antoine Dautry on Unsplash
Python Project: Calculate volume and surface (area) of geometric figures
Task
Write a function "sphere metrics" (radius) that takes a radius as input and returns the volume and surface (area) of a sphere.
This is the code to calculate the sphere volume and area. The documentation is written below.
"""Sphere volume and area"""
# Step 1: import math module
import math
""" Step 2: Create a function called sphere_metrics(radius)
that takes the radius as an argument
and returns both the volume and area of the sphere. """
def sphere_metrics(r):
# Give the volume and area of a sphere
# step 3: define the value of pi using math.pi
pie = math.pi
# step 4: calculate the volume of a sphere
volume = (4 / 3) * pie * r**3
# Step 5: Calculate the Sphere Area
area = 4 * pie * r**2
# Step 6: return the volume and area
return volume, area
# step 7: define the radius of a sphere from user
r = float(input("Enter radius of a sphere"))
# Step 8: Call the function and store the results
# This part, help from CHAT GPT.
sphere_volume, sphere_area = sphere_metrics(r)
# step 9: print the volume of a sphere
print (f'volume = , {sphere_volume}')
print(f'area = , {sphere_area}')
#last minute changes using f strings.
# I call the function I made here.
print(sphere_metrics(r))
The volume of a sphere formula in Python:
Before we code, we need to understand the formula for the volume of a sphere.
Volume (v) is equal to (4/3) multiplied by pie (3.14) multiplied by radius(r) to the power of 3.
- Import the math module.
In Python, we can divide the code into two different parts. Define the pie and the radius. Here, we begin with defining a variable for the “pie”.
- Assign “pie” as the variable for the result of math.pi. The output is 3.14…
math.pi calculates the value of the pie.
- The next part of the equation we need to define is the radius. The radius will be the input.
Start with the input() function. Within the parameter, we write (“Enter radius of sphere”). This will show in the Python Shell when the user inputs a number for the radius. We typecast that data type from a string(automatically defined by Python) to a float. It’s better to choose float instead of integer so it’s more inclusive with any number we type. Assign the whole function to the variable “r”.
- Using the formula 4/3 π r ** 3 for volume, we put all the variables together.
This is what it looks like in Python code.
Divide 4 and 3. It’s written in Python (4/3). Multiply the value of pie(3.14…) using the variable name “pie”. Multiply it to the radius, to the power of 3. Use the ** arithmetic expression for exponential. Assign the result to the variable “v” (volume).
- The result of any input number will be yielded when “y” is printed.
Sample:
The code works for calculating the volume of a sphere of any number input.
Area of a sphere formula in Python:
Explain: 4πr²
- Before we code, we need to understand the formula for the area of a sphere.
Area is equal to 4 multiplied by pie (3.14..) multiplied by radius to the power of 2.
In Python, we use the same code from the volume until Step 3 so we don’t need to rewrite.
We add another code to calculate the area here in Step 5.
Multiply the value of pie(3.14…) using the variable name “pie”. Multiply it to the radius, to the power of 2. Use the ** arithmetic expression for exponential. Assign the result to the variable “a” (area). If we print the values of volume and area, for example for the radius of 5.
This is the result. The calculation of the volume and area works but I need to put all the code into one function.
- Here, I know I need to create and use a def function to be able to perform the calculation of the volume and area of a sphere. But I have never coded one before, so I look for sources online. According to the sources, this is how to make my def function.
“You use functions in programming to bundle a set of instructions that you want to use repeatedly, to carry out a specified task.”
Source: https://www.datacamp.com/tutorial/functions-python-tutorial
I learned that the def function should be at the beginning of my code. So I move this code to Step 2. The sphere_metrics(r) is my function. Takes the “r” as input.
Add the return statement & value (volume, area) to get the results from the def function
I have an indentation error in line 13.
Fix the indentation of all the code for pie, radius, volume and area and return by adding 4 spaces. This is to show what is inside my def function.
Now, I have a problem with my “r” radius variable. So, I take it out of the nested function by deleting 4 spaces.
Now, I have a problem my volume and area are not defined! To fix this problem, sadly, I have to ask ChatGpt because I cannot fix it. Looks like it used the def function and assigned it to the volume and area. Giving the result to sphere_metrics(r). I
Here, I add the print statement to print “sphere_volume” and “sphere_area”. I also add to print the function name “sphere_metrics(r)” just in case I miss something-
Here, I add another way to yield a result. Use the f string to make it shorter.
Volume and area of a cylinder formula in Python:
Task
Write a function cylinder_metrics(radius, height) that takes a radius and height as input and returns the volume and surface (area) of a cylinder.
This is the code to calculate the cylinder volume and area. The documentation is written below.
"""Cylinder volume and area"""
# Step 1: import math module
import math
""" Step 2: Create a function called cylinder_metrics(radius)
that takes the radius and height as an argument
and returns both the volume and area of the cylinder. """
def cylinder_metrics(r, h):
# Give the volume and area of a sphere
# step 3: define the value of pi using math.pi
pie = math.pi
# step 4: calculate the volume of a cylinder
volume = pie * r**2 * h
# Step 5: Calculate the cylinder Area
area = 2 * pie * r * (r + h) # Corrected the syntax here
# Step 6: Return the volume and area
return volume, area
# step 7: define the radius of a sphere from user
r = float(input("Enter radius of a cylinder"))
# step 8: define the radius of a sphere from user
h = float(input("Enter height of a cylinder"))
# Step 9: Call the function and store the results
# This part, help from CHAT GPT.
cylinder_volume, cylinder_area = cylinder_metrics(r, h)
# I call the function I made here.
print(cylinder_metrics(r, h))
# Step 10: Print the results
print(f'volume = {cylinder_volume}')
print(f'area = {cylinder_area}')
#last minute changes using f strings.
Explain: V = π r² h
Volume cylinder = math.pie (3.14) (radius**2) (height)
A = 2 π r(r + h)
Area cylinder = (2) ( math.pie 3.14 ) (radius) (radius + height)
Note: I use the same code from the sphere volume and area code but change a few codes.
Start with importing the math module.
In the def function, change sphere to cylinder. Add height in the function parameter because we will also input height and radius. Change the formula for volume of a cylinder. pie r*2 * h. Add the formula for the area of a cylinder. 2 pie r * h ( r + h). Write the return statement for volume and area of the cylinder_metric(r) function.
For the radius variable input, change the string statement to “cylinder.” Write a new line of code for the height variable input.
Change all the “sphere” to “cylinder”. Add the missing height (h) variable to cylinder_metrics here too.
Add an f string at the end. It will make it easier to format and change the variables.