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...
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.
Bolt: Mass of bolt _8___g Volume of water in graduated cylinder __10__mL Volume of water and...
Bolt: Mass of bolt _8___g Volume of water in graduated cylinder __10__mL Volume of water and bolt in graduated cylinder _13___mL Volume of bolt (subtract the two volumes) _2__mL Density of the bolt _2.67___ g/mL How to calculated the Archimedes’ Method Bolt: Mass of bolt ____g Mass of Displaced Water ____g Calculated Volume of Displaced Water ____mL Density of the bolt ____ g/mL Magnet: Mass of magnet ____g Mass of Displaced Water ____g Calculated Volume of Displaced Water ____mL Density...
A four-cylinder petrol engine has a swept volume of 2000 cm3, and the clearance volume in...
A four-cylinder petrol engine has a swept volume of 2000 cm3, and the clearance volume in each cylinder is 60 cm3. Calculate the air standard cycle efficiency. If the introduction conditions are 1 bar and 24 ?C, and the maximum cycle temperature is 1400 ?C, calculate the mean effective pressure based on the air standard cycle. ANSWERS(59.1%; 5.30 bar)
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT