dazedandconfused - 3 months ago 46

Python Question

"Given sphere_radius and pi, compute the volume of a sphere and assign to sphere_volume. Volume of sphere = (4.0 / 3.0) π r3

Sample output for the given program: 4.18878666667"

I am to get the test to pass both when the radius of 1 is calculated and the radius of 5.5. I can get one or the other to work but not both simultaneously. Please assist me in correcting the code so that it will successfully pass the test to calculate sphere_volumes using the two different radius', 1 and 5.5 when I command it to print(sphere_volume)....

This is what I have and isn't working at all when I combine the two radius calculations in the code:

`pi = 3.14159`

sphere_radius = 1.0

sphere_volume = 0.0

sphere_volume=(((4.0/3.0) * 3.14159) * 1**3)

sphere_radius = 5.5

sphere_volume=(((4.0/3.0) * 3.14159) * 5.5**3)

print(sphere_volume)

Answer

Just add the missing print statement. You have two volumes to report, so you have to print each as soon as calculated.

```
pi = 3.14159
sphere_radius = 1.0
sphere_volume = 0.0
sphere_volume=(((4.0/3.0) * 3.14159) * 1**3)
print(sphere_volume)
sphere_radius = 5.5
sphere_volume=(((4.0/3.0) * 3.14159) * 5.5**3)
print(sphere_volume)
```

Do note that the others have given you several good comments on improving the code:

```
pi = 3.14159
fourth_thirds = 4.0/3.0
sphere_radius = 1.0
sphere_volume = fourth_thirds * pi * sphere_radius**3
print(sphere_volume)
sphere_radius = 5.5
sphere_volume=fourth_thirds * pi * sphere_radius**3
print(sphere_volume)
```

Output:

```
4.1887902047863905
696.9099703213358
```