Home > Software design >  Python coding and testing
Python coding and testing

Time:09-17

Hi there I'm trying to test my code in a separate testing folder but every time i try and perform a python3 test on it, it says NameError: name 'word_count' is not defined even though I've imported the folder that has this name defined as you can see? Can anyone help? `

wordcount code:

def word_count(str): 
counts = dict()
words = str.split()

for word in words:
    if word in counts:
        counts[word]  = 1
    else:
        counts[word] = 1

return counts`

Testing code:

import wordcount 
print(word_count('the quick brown fox jumps over the lazy dog.'))
 

CodePudding user response:

You have to import the function word_count from the module wordcount (if your file is named wordcount.py), i.e.:

from wordcount import word_count
print(word_count('the quick brown fox jumps over the lazy dog.'))

Or use the module name as a prefix for your function

import wordcount
    print(wordcount.word_count('the quick brown fox jumps over the lazy dog.'))

CodePudding user response:

Can you try it with wordcount.word_count(...) if wordcount.py and your test_script.py is in the same folder. Or you can use this import statement: from wordcount import word_count.

  • Related