这是我之前问过的一个问题的副本,但有点不同。如果用户没有输入某个单词,则输出错误信息
我是一个编程初学者(从来没有编程过),我已经给了一个任务(学校)来制作一个程序,要求用户提供一个形状,然后根据给定的尺寸计算该形状的体积用户。但如果用户没有输入“quit”,程序应该不断询问用户形状并继续计算卷,并且当用户输入“quit”时,程序将假设打印出卷的列表计算。三个形状是立方体,金字塔和椭圆体。
,例如,如果在立方体,立方体,棱锥,棱锥形,椭圆体,然后在用户键入退出(与必要的尺寸沿着计算体积),则程序应该打印出:
立方体体积:4 ,5
金字塔卷:6,7
椭球体积:8
注:这些数字只是例如。
我已经成功(有点)让程序注意到错误,并让程序重复询问用户形状和计算体积,直到输入“quit”。但是,如果用户没有例如进入立方体,那么最终的输出应该是:
立方体体积:你有没有做过任何计算多维数据集
金字塔卷:6,7
椭球卷:8
什么,而不是我现在得到,这就是:
立方体的体积:[]
金字塔v olumes:6,7
椭圆形卷:8
有什么办法来实现正确的最终输出?
这是我的代码(这可能不是很大,但它是最好的,我现在可以做一个初学者,用那东西我们至今教授):
#A program that allows the user to continuously pick different shapes and calculate their volumes.
#import all functions from the math module.
import math
#allows the user to input the shape they want to calculate the volume of. Then that input is converted to all upper case
#letters so that no matter what the user enters, it will be recognizable by the program.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
#Initializing the lists for the volumes of the three different shapes as empty lists so that it can be filled
#later on.
VolumeListCube = []
VolumeListPyramid =[]
VolumeListEllipsoid = []
#defining the function that will calculate the volume of the cube and then adding them to the empty list VolumeListCube.
def VolumeCube (sideLength):
volume = sideLength**3
#Adding the values to the list
VolumeListCube.append(volume)
#Sorting the values in the created list in ascending order
VolumeListCube.sort()
return;
#defining the function that will calculate the volume of the pyramid and then adding them to the empty list VolumeListPyramid.
def VolumePyramid (baseLength, height):
volume = round((1/3)*(baseLength**2)*height,1)
#Adding the values to the list
VolumeListPyramid.append(volume)
#Sorting the values in the created list in ascending order
VolumeListPyramid.sort()
return;
#defining the function that will calculate the volume of the ellipsoid and then adding them to the empty list VolumeListEllipsoid.
def VolumeEllipsoid (radius1, radius2, radius3):
volume = round((4/3)*math.pi*radius1*radius2*radius3,1)
#Adding the values to the list
VolumeListEllipsoid.append(volume)
#Sorting the values in the created list in ascending order
VolumeListEllipsoid.sort()
return;
#The first while loop here checks if the user immediately inputs "Quit" or not, if they don't, then the next while loop is
#executed, if they do input "Quit", then the program will print the require message.
while shape != "QUIT":
#This is a infinte while loop since true is always true, this allows the error message at the end to be displayed
#and then loop back to the beginning of this loop, so that it can be executed again.
while True:
if shape in ["CUBE","PYRAMID","ELLIPSOID","QUIT"]:
#These if functions will allow the user to input shape parameters depending on what shape the user has chosen, then
#afterwards, another prompt is show for the user to choose another shape and input that shape's parameters. This will
#continue until the user inputs "Quit", then the required message will be printed and the volume results fot all the
#shapes chosen will be displayed in ascending order.
if shape == "CUBE":
sideLength = int(input("Please enter the length of the sides of the cube:"))
#recalling the function that calculates the volume of the cube.
VolumeCube (sideLength)
#lets the user to input another shape they want to calculate the volume of.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
elif shape == "PYRAMID":
baseLength = int(input("Please enter the base length:"))
height = int(input("Please enter the height:"))
# recalling the function that calculates the volume of the pyramid.
VolumePyramid (baseLength, height)
#lets the user to input another shape they want to calculate the volume of.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
elif shape == "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:"))
# recalling the function that calculates the volume of the ellipsoid.
VolumeEllipsoid (radius1, radius2, radius3)
#lets the user to input another shape they want to calculate the volume of.
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
elif shape == "QUIT":
print ("\nYou have come to the end of the session.\nthe volume calculated for each shape are shown below:\n")
print("The volume of the cube(s) is:", VolumeListCube)
print("The volume of the pyramid(s) is:", VolumeListPyramid)
print("The volume of the ellipsoid(s) is:", VolumeListEllipsoid)
#This exits the program to stop repeated prints that would be caused due to the while loop
exit()
else:
print("Error, please enter a valid shape")
shape = input("please enter the shapes you want to calculate the volume of from cube, pyramid and ellipsoid, or Quit: ").upper()
else:
print ("\nYou have come to the end of the session.\nYou have not performed any volume calculations")
部分是能够打破下来的主要任务分成小块,可以解决并自行完成。我对你提供的代码量感到不知所措,我想你也是。尝试将代码降到最低限度以说明/测试问题。我总是有一个'prototype.py'文件来测试小块。标准概念不需要在主代码库中进行测试,这使事情变得艰难。 – roganjosh
谢谢您的评论!我会尽力做到这一点,从现在开始。对不起,如果我看起来像一个noob,我只是刚刚开始编码像一个月前,只知道一点。不过谢谢你的提示^^ – Pengibaby