-1

just wondering how I can create two different python files one being main.py and the other being volume.py that creates the same output as the code being in a singular file.

My code is

def cubeVolume() :
    sideLength = float(input('What is the length of one side of the cube?: '))
    volume = sideLength ** 3
    cubeVolumeList.append(round(volume, 1))     # 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, 1), round(volume, 1)))

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, 1))
    pyramidVolumeList.sort()
    print('\nThe volume of a pyramid with a base length of %.1f and a height of %.1f is %.1f' % (round(base, 1), round(height, 1), round(volume, 1)))

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, 0))
    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, 1), round(r2, 1), round(r3, 1), round(volume, 1)))

# 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

But I want to separate the volume part and the main part into two different files, I tried splitting it and importing volume.py onto main.py but I'm getting an error saying 'cubeVolume' is not defined. Help would be appriciated.

Avey V
  • 1

2 Answers2

0

Take the functions you want to separate and put them into your volume.py file. Place it in the same directory.

Then simply put this at the top of main.py:

from volume import cubeVolume, pyramidVolume, ellipsoidVolume

Then those functions should be available in main.py.

Colin
  • 4,025
  • 21
  • 40
0

Is good that you put the functions in a file volume.py and then import from main.py. Your problem is that initialization should be done inside the functions. Otherwise the var is undeclared. That happens cause you use global variables. Your next step is learning passing parameters and return variables. Keep going, you're doing great!. :)

Example:

def cubeVolume() :
    cubeVolumeList = []
    sideLength = float(input('What is the length of one side of the cube?: '))
    volume = sideLength ** 3
    cubeVolumeList.append(round(volume, 1))     # 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, 1), round(volume, 1)))

Cheers

Carles Mateo
  • 199
  • 1
  • 7