如果用户没有输入某个单词则打印一条错误消息

Printing an error message if a user does not input a certain word

这有点像我之前问过的问题,但有点不同。

我是一名编程初学者(以前从未编程过),我接到了一项任务(在学校)制作一个程序,要求用户提供一个或多个形状,然后根据给定的尺寸计算该形状的体积由用户。然而,如果用户不输入 "quit",程序应该继续询问用户形状并继续计算体积,当用户输入 "quit" 时,程序应该会打印出来计算的体积列表。三种形状分别是立方体、金字塔和椭圆体。

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

立方体积:4、5

金字塔体积:6、7

椭圆体积:8

注意:这些数字仅供参考。

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

多维数据集体积:您尚未对多维数据集进行任何计算

金字塔体积: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")

TL;DR

没有时间查看您的代码,但我猜您得到的最终结果是 list,因此

cube volumes: []

对此的快速逃避是使用 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

应该足够了。