[Tutor] __init__ doesn't seem to be running
Cameron Macleod
cmacleod170 at googlemail.com
Fri Mar 15 22:21:54 CET 2013
Hello everyone, I'm using Python 3.3 and am trying to write a simple to-do
list program. I have a class which runs pretty much everything called todo
and the __init__ method doesn't seem to be running.
class todo():
def __init__(self):
tasks = []
def writeTask(todoList):
name = input("Please enter the task > ")
desc = input("Please enter a short description (optional) > ")
while True:
try:
imp = int(input("How important is this task? 1-100 > "))
break
except TypeError:
imp = int(input("How important is this task? 1-100 > "))
if imp > 100:
imp = 100
elif imp < 1:
imp = 1
todoList.write("\"" + name + "\",\"" + desc + "\",\"" + str(imp) +
"\"")
print("Task written!")
try:
todoList = open('todo.txt', 'r+')
except IOError:
todoList = open('todo.txt', 'w')
for line in todoList:
tasks.append(line.strip())
writeTask(todoList)
todoList.close()
main = todo()
Whenever I run this, I get the error:
Traceback (most recent call last):
File "C:\Python33\todo.py", line 8, in <module>
class todo():
File "C:\Python33\todo.py", line 34, in todo
tasks.append(line.strip())
NameError: name 'tasks' is not defined
Indicating that __init__ hasn't run since that is the initialization for
tasks. I had always understood that __init__ was the equivalent of a
constructor, and should be run at the instantiation of any class.
Any help would be much appreciated,
Cameron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130315/17990d3e/attachment.html>
More information about the Tutor
mailing list