(Beginner) Error when I type 'quit' on volume calculator

62 Views Asked by At

I'm fairly new to python. Just wondering why my volume calculator has an error when I type 'quit'. All the other equations such as cube, pyramid and ellipsoid work.. but when I type 'quit' or 'q' to give me back the answers I entered there's an error.

This is the code

 def cubeVolume() :
    sideLength = float(input('What is the length of one side of the cube?: '))
    volume = sideLength ** 3
    cubeVolumeList.append(round(volume, 2))     # Adds the rounded volume to the given list
    cubeVolumeList.sort()
    print('\nThe volume of a cube with a side length of %.1f is %.1f' % (round(sideLength, 2), round(volume, 2)))

def pyramidVolume() :
    base = float(input('What is the base length of the pyramid?: '))
    height = float(input('What is the height of the pyramid?: '))
    volume = (1 / 3) * (base ** 2) * height
    pyramidVolumeList.append(round(volume, 2))
    pyramidVolumeList.sort()
    print('\nThe volume of a pyramid with a base length of %.1f and a height of %.1f is %.1f' % (round(base, 2), round(height, 2), round(volume, 2)))

def ellipsoidVolume() :
    from math import pi
    r1 = float(input('What is the value of radius 1?: '))       # r1: radius 1, r2: radius 2, etc.
    r2 = float(input('What is the value of radius 2?: '))
    r3 = float(input('What is the value of radius 3?: '))
    volume = (4 / 3) * pi * r1 * r2 * r3
    ellipsoidVolumeList.append(round(volume, 2))
    ellipsoidVolumeList.sort()
    print('\nThe volume of an ellipsoid with a radius 1 of %.1f, a radius 2 of %.1f, and a radius 3 of %.1f is %.1f' % (round(r1, 2), round(r2, 2), round(r3, 2), round(volume, 2)))

# Initialization of empty lists

cubeVolumeList = []
pyramidVolumeList = []
ellipsoidVolumeList = []

# Creating the main function to be called at the end of the program

def main() :
    shapeInput = (input('Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e):')).lower()

    # Creation of validity loop

    valid = False
    while not valid :
        if shapeInput in ['c', 'cube']:
            cubeVolume()
            print()
            shapeInput = (input('Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e):')).lower()
        elif shapeInput in ['p', 'pyramid']:
            pyramidVolume()
            print()
            shapeInput = (input('Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e):')).lower()
        elif shapeInput in ['e', 'ellipsoid'] :
            ellipsoidVolume()
            print()
            shapeInput = (input('Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e):')).lower()
        elif shapeInput in ['q', 'quit'] :
            valid = True
        else :
            print('\nInvalid input! Please try again.')
            print()
            shapeInput = (input('Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e):')).lower()

    # Output after user enters "quit"

    if len(cubeVolumeList) != 0 or len(pyramidVolumeList) != 0 or len(ellipsoidVolumeList) != 0 :
        print()
        print('You have come to the end of the session.')
        print('The volumes calculated for each shape are shown below.')

        print('cube: ', end = '')
        i = 0
        while i < len(cubeVolumeList) - 1 :
            print(str(cubeVolumeList[i]) + ',' + ' ', end = '')
            i = i + 1
        print(cubeVolumeList[len(cubeVolumeList) - 1])

        print('pyramid: ', end = '')
        i = 0
        while i < len(pyramidVolumeList) - 1 :
            print(str(pyramidVolumeList[i]) + ',' + ' ', end = '')
            i = i + 1
        print(pyramidVolumeList[len(pyramidVolumeList) - 1])

        print('ellipsoid: ', end = '')
        i = 0
        while i < len(ellipsoidVolumeList) - 1 :
            print(str(ellipsoidVolumeList[i]) + ',' + ' ', end = '')
            i = i + 1
        print(ellipsoidVolumeList[len(ellipsoidVolumeList) - 1])

    else :
        print('\nYou have come to the end of the session.')
        print('You did not perform any volume calculations')

main()  # Calling the main function

Not sure why when I type 'quit' there's an error. Help would be greatly be appreciated. Thanks

The error message is

Traceback (most recent call last):
  File "C:/Users/aman/PycharmProjects/Assign1.py/main.py", line 95, in <module>
    main()  # Calling the main function
  File "C:/Users/aman/PycharmProjects/Assign1.py/main.py", line 82, in main
    print(pyramidVolumeList[len(pyramidVolumeList) - 1])
IndexError: list index out of range
1

There are 1 best solutions below

0
On

You handle the case where a user enters a session without calculating some values with the following line: if len(cubeVolumeList) != 0 or len(pyramidVolumeList) != 0 or len(ellipsoidVolumeList) != 0 : The problem is that this check only tells you that at least one of the volumes was calculated but not which one. The subsequent code in that block assumes that all volumes were calculated. To fix, you can add an individual check to see which list is empty:

...

    if len(cubeVolumeList) != 0 or len(pyramidVolumeList) != 0 or len(ellipsoidVolumeList) != 0 :
        print()
        print('You have come to the end of the session.')
        print('The volumes calculated for each shape are shown below.')

        print('cube: ', end = '')
        if len(cubeVolumeList) == 0:
            print('NIL')
        else:
            i = 0
            while i < len(cubeVolumeList) - 1 :
                print(str(cubeVolumeList[i]) + ',' + ' ', end = '')
                i = i + 1
            print(cubeVolumeList[len(cubeVolumeList) - 1])

        print('pyramid: ', end = '')
        if len(pyramidVolumeList) == 0:
            print('NIL')
        else:
            i = 0
            while i < len(pyramidVolumeList) - 1 :
                print(str(pyramidVolumeList[i]) + ',' + ' ', end = '')
                i = i + 1
            print(pyramidVolumeList[len(pyramidVolumeList) - 1])

        print('ellipsoid: ', end = '')
        if len(ellipsoidVolumeList) == 0:
            print('NIL')
        else:
            i = 0
            while i < len(ellipsoidVolumeList) - 1 :
                print(str(ellipsoidVolumeList[i]) + ',' + ' ', end = '')
                i = i + 1
            print(ellipsoidVolumeList[len(ellipsoidVolumeList) - 1])

    else :
        print('\nYou have come to the end of the session.')
        print('You did not perform any volume calculations')