简体   繁体   中英

Keep getting an error when trying to import my own module

I have this simple function that takes a list and returns the sum of all the elements. I'm trying to import this to another program.

Here are my two programs:

calculate.py

def addition(numList):
    theSum = 0
    for i in numList:
        theSum = theSum + i
    return theSum

and:

import calculate

addition([1,3,5,7,9])

Expected output is 25 but I just get the error: 'module' object is not callable' What am I doing wrong?

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    addition([1,3,5,7,9])
NameError: name 'addition' is not defined

Created a file calculate.py and test.py for running your code, the problem here is you are not importing correctly. What you should do is from calculate import * imports all functions inside calculate.py or preferably from calculate import addition for a single function

from calculate import *
# or 
from calculate import addition

addition([1,3,5,7,9])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM