Pengibaby - 3 years ago 155

Python Question

I am a programming beginner (never programmed before) and I have been given a task (for school) to make a program that asks the user for a shape(s), then calculate that shape's volume from the dimensions given by the user. however if the user does not type in "quit", the program should keep asking the user for shapes and keep on calculating the volumes, and when the user do type in "quit", the program is suppose to print out the list of volumes calculated. the three shapes are Cube, pyramid and ellipsoid.

for example, if the user types in **cube, cube, pyramid, pyramid, ellipsoid** and then quit (along with the necessary dimensions to calculate volume), then the program should print out:

*cube volumes is 4, 5*

*pyramid volumes is 6, 7*

*ellipsoid volumes is 8*

NOTE: these numbers are just for example.

I have succeeded (kinda) in getting the program to notice errors and for the program to repeatedly ask the user for shapes and calculate volumes until "quit" has been entered, however I cannot figure out how to achieve the "example list type of answers", is there a way to do this?

This is my code (it's probably not that great, but it's the best I can do right now as a beginner and with the stuff we are taught so far):

`import math`

`shapeName = input ("Please enter the shape you want to calculate the volume`

`of:").upper()`

`def volumeCube (side):`

Volume = 0

Volume = side**3

print ("The Volume of the cube is", Volume)

return;

def volumePyramid (baseSideLength,height):

Volume = 0

Volume = round((1/3)*(baseSideLength**2)*height,1)

print ("The volume of the pyramid is", Volume)

return;

def volumeEllipsoid (radius1,radius2,radius3):

Volume = 0

Volume = round((4/3)*math.pi*radius1*radius2*radius3,1)

print ("The volume of the ellipsoid is", Volume)

return;

while shapeName != "QUIT":

while shapeName in ["CUBE","PYRAMID","ELLIPSOID","QUIT"]:

if shapeName == "CUBE":

side = int(input("Please enter the length of the sides:"))

volumeCube(side)

shapeName = input("Please enter the shape you want to calculate the volume of:").upper()

elif shapeName == "PYRAMID":

baseSideLength = int(input("Please enter the lengths of the side of the base:"))

height = int(input("Please enter the height of the pyramid:"))

volumePyramid(baseSideLength, height)

shapeName = input("Please enter the shape you want to calculate the volume of:").upper()

elif shapeName == "ELLIPSOID":

radius1 = int(input("Please enter the first radius:"))

radius2 = int(input("Please enter the second radius:"))

radius3 = int(input("Please enter the third radius:"))

volumeEllipsoid(radius1, radius2, radius3)

shapeName = input("Please enter the shape you want to calculate the volume of:").upper()

elif shapeName == "QUIT" :

print ("\nYou have come to the end of the session. \nThe volumes calculated for each shape are shown below:")

volumeCube (side)

volumePyramid(baseSideLength, height)

volumeEllipsoid(radius1, radius2, radius3)

exit()

else:

print ("Error")

shapeName = input("Please enter the shape you want to calculate the volume of:").upper()

else:

print ("\nyou have come to the end of the session. \nYou have not performed any volume calculations.")

This is my original code which didn't create lists, so i tried to test it out by just changing the "cube" part of the function like:

`def volumeCube (side):`

Volume = 0

VolumeCubeList = []

VolumeCubeList.append (side**3)

print (VolumeCubeList)

return;

but this just returned one answer (e.g. if the side length was 3 for the first cube and 4 for the second cube, the answer returned was only [64])

Is there a way to fix this? Is there anything else i am doing wrong?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

it seems the problem is because some of your variables are not defined.

When executing this code

```
elif shapeName == "QUIT" :
print ("\nYou have come to the end of the session. \nThe volumes calculated for each shape are shown below:")
volumeCube (side)
volumePyramid(baseSideLength, height)
volumeEllipsoid(radius1, radius2, radius3)
exit()
```

Why it doesnt work ?

For example, if the user did not want to calculate the volumePyramid, you are still calling volymePyramid(BaseSideLenght, height), basesideLenght and height are never defined because they were never entered by the user. (and its the same for the other shapes)

What you could do is have an list of strings that store a volume every time you calculate one and show this list at the end of your program ;)

Here is how to do so roughly :

```
import math
shapeName = input ("Please enter the shape you want to calculate the volume of:").upper()
myList = []
def volumeCube (side):
Volume = 0
Volume = side**3
print ("The Volume of the cube is", Volume)
myList.append("The volume of the cube was "+ str(Volume))
return;
def volumePyramid (baseSideLength,height):
Volume = 0
Volume = round((1/3)*(baseSideLength**2)*height,1)
print ("The volume of the pyramid is", Volume)
myList.append("The volume of the pyramid was "+ str(Volume))
return;
def volumeEllipsoid (radius1,radius2,radius3):
Volume = 0
Volume = round((4/3)*math.pi*radius1*radius2*radius3,1)
print ("The volume of the ellipsoid is", Volume)
myList.append("The volume of the ellipsoid was " + str(Volume))
return;
def printArray ():
for word in myList:
print(word)
while shapeName != "QUIT":
while shapeName in ["CUBE","PYRAMID","ELLIPSOID","QUIT"]:
if shapeName == "CUBE":
side = int(input("Please enter the length of the sides:"))
volumeCube(side)
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
elif shapeName == "PYRAMID":
baseSideLength = int(input("Please enter the lengths of the side of the base:"))
height = int(input("Please enter the height of the pyramid:"))
volumePyramid(baseSideLength, height)
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
elif shapeName == "ELLIPSOID":
radius1 = int(input("Please enter the first radius:"))
radius2 = int(input("Please enter the second radius:"))
radius3 = int(input("Please enter the third radius:"))
volumeEllipsoid(radius1, radius2, radius3)
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
elif shapeName == "QUIT" :
print ("\nYou have come to the end of the session. \nThe volumes calculated for each shape are shown below:")
printArray()
exit()
else:
print ("Error")
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
else:
print ("\nyou have come to the end of the session. \nYou have not performed any volume calculations.")
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**