在列表中打印计算值
Printing calculated values in a list
我是一名编程初学者(以前从未编程过),我接到了一项任务(在学校)制作一个程序,要求用户提供一个或多个形状,然后根据给定的尺寸计算该形状的体积由用户。然而,如果用户没有输入 "quit",程序应该继续询问用户形状并继续计算体积,当用户输入 "quit" 时,程序应该会打印出来计算的体积列表。三种形状分别是立方体、金字塔和椭圆体。
例如,如果用户输入 立方体、立方体、金字塔、金字塔、椭球体然后退出(以及计算体积所需的尺寸),则程序应该打印出来:
立方体积为4, 5
金字塔体积为6、7
椭球体积为 8
注意:这些数字仅供参考。
我已经成功(有点)让程序注意到错误,并让程序反复询问用户形状和计算体积,直到输入 "quit",但是我不知道如何实现"example list type of answers",有办法吗?
这是我的代码(它可能不是那么好,但这是我作为初学者和目前所学知识所能做到的最好的):
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.")
这是我的原始代码,它没有创建列表,所以我尝试通过更改函数的 "cube" 部分来测试它,例如:
def volumeCube (side):
Volume = 0
VolumeCubeList = []
VolumeCubeList.append (side**3)
print (VolumeCubeList)
return;
但这只返回一个答案(例如,如果第一个立方体的边长为 3,第二个立方体的边长为 4,则返回的答案仅为 [64])
有没有办法来解决这个问题?还有什么我做错了吗?
看来问题是因为执行以下代码时您的某些变量未定义:
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()
现在,为什么它不起作用?
因为,例如,如果用户不想计算 volumePyramid,您仍在调用 volymePyramid(BaseSideLenght, height),basesideLenght 和 height 从未被定义,因为它们从未被用户输入。 (其他形状也一样)
你可以做的是有一个字符串列表,每次你计算一个时都会存储一个体积,并在你的程序结束时显示这个列表;)
操作方法如下:
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.")
我是一名编程初学者(以前从未编程过),我接到了一项任务(在学校)制作一个程序,要求用户提供一个或多个形状,然后根据给定的尺寸计算该形状的体积由用户。然而,如果用户没有输入 "quit",程序应该继续询问用户形状并继续计算体积,当用户输入 "quit" 时,程序应该会打印出来计算的体积列表。三种形状分别是立方体、金字塔和椭圆体。
例如,如果用户输入 立方体、立方体、金字塔、金字塔、椭球体然后退出(以及计算体积所需的尺寸),则程序应该打印出来:
立方体积为4, 5
金字塔体积为6、7
椭球体积为 8
注意:这些数字仅供参考。
我已经成功(有点)让程序注意到错误,并让程序反复询问用户形状和计算体积,直到输入 "quit",但是我不知道如何实现"example list type of answers",有办法吗?
这是我的代码(它可能不是那么好,但这是我作为初学者和目前所学知识所能做到的最好的):
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.")
这是我的原始代码,它没有创建列表,所以我尝试通过更改函数的 "cube" 部分来测试它,例如:
def volumeCube (side):
Volume = 0
VolumeCubeList = []
VolumeCubeList.append (side**3)
print (VolumeCubeList)
return;
但这只返回一个答案(例如,如果第一个立方体的边长为 3,第二个立方体的边长为 4,则返回的答案仅为 [64]) 有没有办法来解决这个问题?还有什么我做错了吗?
看来问题是因为执行以下代码时您的某些变量未定义:
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()
现在,为什么它不起作用?
因为,例如,如果用户不想计算 volumePyramid,您仍在调用 volymePyramid(BaseSideLenght, height),basesideLenght 和 height 从未被定义,因为它们从未被用户输入。 (其他形状也一样)
你可以做的是有一个字符串列表,每次你计算一个时都会存储一个体积,并在你的程序结束时显示这个列表;)
操作方法如下:
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.")