Volume of Sphere in Python

Problem Description:

You are given a positive integer r denoting the radius of a sphere as a parameter. Write a program to calculate the volume of the sphere. The volume of a sphere having radius R is given by (4 * π * R3) / 3.

NOTE: Return the volume of the sphere up to two decimal places. You can use round().

NOTE2: Use pi as 22/7 (not math.pi).

Input Format:

The first line indicates the number of the test cases. For each testcase there will be one line of input: The one line contains r in integer format.

Output Format:

The volume of the sphere in float format is printed for each testcase in a new line.

Sample Input:

1 8

Sample Output:

2145.52

Seems straightforward.

The mistakes I see are that you wrote `R3` instead of `R**3` (which is `R * R * R`) and for the sample input you wrote `1 8` when it should be `8` to give that output.

I am really sorry back there is a mistake but I would like to inform you start I have just copy pasted the question which is given on the scaler website on which I’m taking the course also I really appreciate that you actually tried helping me but but could you please provide me the answer put question if you have understood it.

All the elements that you need are right there and Matthew has even corrected it for you.

Maybe it’s a case of ‘the first line of code is the hardest’, so here’s a start, but the best way to learn, is to try, make mistakes, learn from them and try again.

``````pi = 22 / 7
r = 8
``````

Now, all you need is ` v =` and a call to the `print()` function. Don’t worry too much about the rounding for now; we can get to that.

Is there any technique which I can learn to understand the questions first and then try to solve it in the programming language? Well, the question is more a math question than a Python one, so a quick internet search will result in pages of hits. But you don’t need to do that, because as I’ve said, you already have all the element that you need.

Without the rounding, you should be able to have a simple script (no more than four line of code, of which I’ve given you two) that will display `2145.5238095238096`, given that the radius ‘r’ is equal to 8.

1 Like

No. We’re not here to do your homework for you. People have attempted to help you to understand the question, but ultimately, this is for you to solve, not us.

4 Likes

What exactly don’t you understand about the question?

Do you know what a sphere is?
Do you know what “volume” means?
Do you know what “radius” means?
Do you understand how to find the volume of a sphere, if you know its radius?

3 Likes

I wrote the answer but I thought it was best to delete it. What rob42 said was best. They gave you what v is, just have to plug 22/7 for pi. Make r into an input function which I am assuming you can do then use the print function and round volume by how many decimals the question tells you to. Hope this helps

1 Like

Thank you so much for your valuable reply, and yes I have already solved the question. 1 Like