Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing modules into python shell

Tags:

python

shell

Im trying to test my files directly from the python shell instead of running it in my .py file. However whenever I import the module and call a function it says NameError: name 'evaluate_essay' is not defined even though it is defined. How do I fix this?

Here is the code for the program:

def evaluate_essay(essayFilename):
    fileList= []
    file= open(essayFilename, "r")
    fileList= [file.read().split()]
    file.close()
    longWords=0
    medWords=0
    shortWords=0
    #nested for loop that checks every word in list
    for i in range (len(fileList)):
        for k in range (len(fileList[0])):
            if (len(fileList[0][k])) >= 7:
                longWords += 1
            if 4<=(len(fileList[0][k]))<=6:
                medWords += 1
            if (len(fileList[0][k])) <= 3:
                shortWords += 1
    #if statements that determines level of each essay
    if (longWords) >= (len(fileList[0])/2):
        print ("This is a COLLEGE LEVEL essay")
    elif (longWords)>(medWords) and (longWords)>(shortWords):
        print ("This is a HIGH SCHOOL LEVEL essay")
    elif (medWords)>(longWords) and (medWords)>(shortWords):
        print ("This is a MIDDLE SCHOOL LEVEL essay")
    else:
        print ("This is an ELEMENTARY SCHOOL LEVEL essay")

evaluate_essay()
like image 864
Etty Ets Avatar asked Sep 05 '26 09:09

Etty Ets


1 Answers

First of all, remove the call to evaluate_essay from the script, or give it an argument. You're currently calling it without an argument, but it requires one.

To call this function from an interactive session, you first need to import the module. One of the following conditions must be met.

  1. Your interpreter session is running in the directory containing the module.
  2. The module is in a directory that's in your PYTHONPATH.

Now, in the interpreter, there are two approaches. One is to import the module and prefix references to its attributes with its name, like so.

import problem3
problem3.evaluate_essay(my_file_name)

The other approach is to explicitly import the function and use its name unqualified.

from problem3 import evaluate_essay
evaluate_essay(my_file_name)
like image 165
Sam Marinelli Avatar answered Sep 06 '26 21:09

Sam Marinelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!