Search code examples
pythonpython-3.xlistloopserror-messaging

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


This is kinda of a copy of a question I asked before, but kind of different.

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: 4, 5

pyramid volumes: 6, 7

ellipsoid volumes: 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, if the user does not enter cube for example, then the final output should be:

cube volumes: You have not done any calculations for the cube

pyramid volumes: 6, 7

ellipsoid volumes: 8

instead of what I am getting right now, which is:

cube volumes: []

pyramid volumes: 6, 7

ellipsoid volumes: 8

Is there any way to achieve the correct final output?

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):

#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")

Solution

  • TL;DR

    Don't have time to go through your code, but am guessing that you are getting the final results as a list, and so the

    cube volumes: []

    The quick escape for this would be to use an if statement to check if the size of the list is 0. ie, user did not give that shape.

    So, a simple :

    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
    

    should suffice.