0

这有点像我之前问的一个问题的副本,但有点不同。

我是一名编程初学者(以前从未编程过),并且我被赋予了一项任务(对于学校)来制作一个程序,该程序要求用户提供一个或多个形状,然后根据用户给出的尺寸计算该形状的体积。但是,如果用户没有输入“退出”,程序应该不断询问用户形状并继续计算体积,当用户输入“退出”时,程序应该打印出体积列表计算出来的。三种形状是立方体,金字塔和椭圆体。

例如,如果用户输入立方体、立方体、金字塔、金字塔、椭圆体然后退出(以及计算体积所需的尺寸),那么程序应该打印出:

立方体体积:4、5

金字塔卷:6、7

椭球体积:8

注意:这些数字只是举例。

我已经成功(有点)让程序注意到错误并让程序反复询问用户形状并计算体积,直到输入“退出”。但是,如果用户没有输入立方体,那么最终输出应该是:

立方体体积:您还没有对立方体进行任何计算

金字塔卷:6、7

椭球体积:8

而不是我现在得到的,即:

立方体体积:[]

金字塔卷: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")
4

1 回答 1

1

TL;博士

没有时间浏览你的代码,但我猜你得到的最终结果是 a list,所以

立方体体积:[]

对此的快速逃避是使用if语句来检查列表的大小是否为0. 即,用户没有给出那个shape

所以,一个简单的:

print('cube volumes:',end=' ')

#VolumeListCube is holding the final results
if len(VolumeListCube) == 0:
   print("You have not done any calculations for the cube")
else :
   #print your values as usual

应该足够了。

于 2017-10-17T19:00:21.177 回答