Question

In: Computer Science

The expression below (approximately) calculates the volume of a cylinder. Extract the expression into a function...

The expression below (approximately) calculates the volume of a cylinder. Extract the expression into a function named calculate_volume, and then replace the existing calculations with function calls.

Do in Python

Solutions

Expert Solution

The expression written in the given print() calls are extracted into the function calculate_volume() and the existing calculations in the print() are replaced by the

function calls.

The complete code is given as follows:

Screenshot of the code:

Sample Output:

Code to copy:

#Define the function calculate_volume() having two parameters

#radius and height.

def calculate_volume(radius, height):

   

    #Initialize a variable PI with the value 3.14159.

    PI = 3.14159

    #Calculate the volume of cylinder by using the

    #formula PI * (radius^2) * height by placing the

    #value of PI, radius, and height value in this

    #formula.

    volumeOfCylinder = PI * (radius ** 2) * height

   

    #Return the volume of cylinder calculated above.

    return volumeOfCylinder

#Call the function calculate_volume() by passing different

#arguments.

print(calculate_volume(1, 2))

print(calculate_volume(5, 1))

print(calculate_volume(2, 3.1))


Related Solutions

1)Choose all of the statements below that would change the volume of gas depicted in Cylinder...
1)Choose all of the statements below that would change the volume of gas depicted in Cylinder A to that of Cylinder B. a□ Increase the pressure by a factor of 6 while holding the Kelvin temperature and the moles of gas constant. b□Reduce the pressure by a factor of 6 while holding the temperature and the moles of gas constant. c□Increase the Kelvin temperature by a factor of 6 while holding the pressure and the moles of gas constant. d□Simultaneously...
Consider E as function of the volume and temperature. Write down and expression for dE(V,T). Identify...
Consider E as function of the volume and temperature. Write down and expression for dE(V,T). Identify the partial derivatives in terms of known / measurable quantities if possible.
A cylinder has a radius of x + 2 units and a height of 3 units greater. Express the volume of the cylinder as a polynomial function. For the following exercises, write the polynomial function that models the given situation.
For the following exercises, write the polynomial function that models the given situation. A cylinder has a radius of x + 2 units and a height of 3 units greater. Express the volume of the cylinder as a polynomial function.
find the dimensions and volume of the right circular cylinder of maximum volume inscribed in a...
find the dimensions and volume of the right circular cylinder of maximum volume inscribed in a sphere with radius 50cm
Make a function that calculates the summation of even numbers in the range. The function sum...
Make a function that calculates the summation of even numbers in the range. The function sum takes the two integer parameters and they are used as the range. The function uses default parameters for the range. When we call this function with one argument, it will be used as a starting point and the end of the range will be 100. Also, the function is called without any argument, the default range (0,100) will be used. We will use default...
Question4: Create and use a lambda expression which calculates the sum of three numbers. Ask the...
Question4: Create and use a lambda expression which calculates the sum of three numbers. Ask the user for three numbers and use the lambda function to print the sum [6 marks] Question5: Write a generator function which generates the Fibonacci series. [0,1,2,4,8,16,…]. The Fibonacci series is a list of numbers where each number is the sum of the previous two. [3 marks] Then write the code to iterate through the first six numbers of sequence. [3 marks] Question6: Write a...
The volume of some air, assumed to be an ideal gas, in the cylinder of a...
The volume of some air, assumed to be an ideal gas, in the cylinder of a car engine is 540cm^3 at a pressure of 1.1 x 10^5 Pa and a Temperature of 27 Degree celsius. The air is suddenly compressed, so that no thermal energy enters or leaves the gas, to a volume of 30cm^3. The pressure rises to 6.5 x 10^6 Pa. Determine the Temperature of the gas after compression.
8. Complete the program that calculates the volume of a cube. If the side length entered...
8. Complete the program that calculates the volume of a cube. If the side length entered for the cube is negative, the program should display an error message saying the length should be positive. If the side length entered for the cube is greater than 100, the program should print a message saying the side is too big. Otherwise the program should calculate and print the volume of the cube of the given side length. Sample program runs are shown...
Write a program that reads the radius of a sphere and calculates it's volume and surface...
Write a program that reads the radius of a sphere and calculates it's volume and surface area. Format results to 4 decimal places. The radius=3.2 Your program should output volume and surface area.  Repeat same steps for program2, but this time use the following input values: a= ꟷ2.4 , b = 4.5 . Your program should output sum, Fun1, and Fun2. It's for C++
Read the function definitions of the two functions: “compute_st_ave” and “compute_quiz_ave”. Write a function that calculates...
Read the function definitions of the two functions: “compute_st_ave” and “compute_quiz_ave”. Write a function that calculates the average of student averages. (Student averages are stored in st_ave [5] array). Include a statement to print this average on the screen. Declare this function above the “main” function where other functions are declared. In the main program, invoke this function. //Reads quiz scores for each student into the two-dimensional array grade //(but the input code is not shown in this display). Computes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT