0
def volumeofcube():
    a = int(input('Enter the length of the side:'))
    volume = a**3
    #Rounded to one decimal place
    volume = str(round(volume, 1))
    #The volume is added to its corresponding list
    volumeofcubelist.append(volume)
    print('The volume of the cube with the side', a,'is', volume)
    return volume

this is a funtion that i want to import into another file (main.py) to work like this:

elif shape == 'cube':
    volumeofcube()

however when I try to import:

import volumes
or
from volumes import volumeofcube()

none of these are able to find the volumes.py module and import it

  • 1
    `from volumes import volumeofcube` is the correct syntax for what you are trying to do. – zvone Oct 24 '18 at 20:42
  • Possible duplicate of [How to import other Python files?](https://stackoverflow.com/questions/2349991/how-to-import-other-python-files) – Oliver Baumann Oct 24 '18 at 20:43
  • just `from volumes import volumeofcube` without `()` will do if your .py is named `volume` – deadvoid Oct 24 '18 at 20:45

1 Answers1

1

If you are trying to import as

import volumes

the call for volumeofcube method should be

volumes.volumeofcube()

if you try to import as

from volumes import volumeofcube()

you need to take the parenthesis off, the right syntax is:

from volumes import volumeofcube
Rodolfo Donã Hosp
  • 1,037
  • 1
  • 11
  • 23
  • The correct syntax in the second case would be just `from volumes import volumeofcube` (i.e. with no parentheses at the end). – martineau Oct 24 '18 at 21:42