views:

17

answers:

1

i have the following code in a module called code_database.py

class Entry():
    def enter_data(self):
        self.title = input('enter a title: ')
        print('enter the code, press ctrl-d to end: ')
        self.code = sys.stdin.readlines()
        self.tags = input('enter tags: ')

    def save_data(self):
        with open('entry.pickle2', 'ab') as f:
            pickle.dump(self, f)

in idle the class-defined methods work fine:

>>> import code_database
>>> entry = code_database.Entry()
>>> entry.enter_data()
enter a title: a
enter the code, press ctrl-d to end: 
benter tags: c
>>> entry.title
'a'
>>> entry.code
['b']
>>> entry.tags
'c'
>>> 

however if i call the module from an external program and try to call the methods, they raise a NameError:

import code_database

    entry = code_database.Entry()
    entry.enter_data()
    entry.save_data()

causes this in the terminal:

$python testclass.py 
enter a title: mo
Traceback (most recent call last):
  File "testclass.py", line 6, in <module>
    entry.enter_data()
  File "/home/mo/python/projects/code_database/code_database.py", line 8, in enter_data
    self.title = input('enter a title: ')
  File "<string>", line 1, in <module>
NameError: name 'mo' is not defined
+3  A: 

You're using python-2.x when running your testclass.py file. Your code, however, seems to be written for python-3.x version. In python-2.x you need to use raw_input functions for the same purpose you would use input in python-3.x. You could run

$ python --version

To find out what exactly version you're using by default.

SilentGhost
haha, not enough coffee today i guess... that was bang on.
momo