I am working on the Advent of Code challenges, and Day 7 has proven a little difficult. I decide to create a class directory() with properties- or whatever the technical python term is- name, contents, and sum. Name is a name, contents is a list of “files” in the directory, which contains other directories and strings, and sum is unused, but will have a future use. My question is why does line 31 give me TypeError: cannot unpack non-iterable NoneType object
class directory():
name = ""
# used to identify the directory
contents = []
sum = 0
file = open("input.txt")
moreLines = True
slash = directory()
slash.name = "/"
def readDir(file, dir, line):
newLine = ""
for i in line:
if i != line[0] and i != line[1] and i != line[2] and i != line[3] and i != line[len(line)-1]:
newLine += i
if dir.name == newLine:
temp = file.readline()
temp = file.readline()
if temp[0] == "$":
return dir, temp
else:
slash.contents.append(temp)
line = file.readline()
slash, line = readDir(file,slash,line)```
Thank you!
That’s what I was afraid of, I’m still learning, which, I suppose is what replit ask is for.
I actually had read through the W3Schools page previously, but I didn’t really think I understood. Seeing how you would do it in my place has confirmed it to me though, so thank you very much.
Could you or someone else verify that my new updated code is correct? I’m still getting the same error.
class directory():
def __init__(self,nomen):
self.name = nomen
self.contents = []
self.sum = 0
file = open("input.txt")
moreLines = True
slash = directory("/")
def readDir(file, dir, line):
newLine = ""
for i in line:
if i != line[0] and i != line[1] and i != line[2] and i != line[3] and i != line[len(line)-1]:
newLine += i
if dir.name == newLine:
temp = file.readline()
temp = file.readline()
if temp[0] == "$":
return dir, temp
else:
if temp[0] == "d":
newDir = temp
slash.contents.append(newDir)
else:
slash.contents.append(temp)
line = file.readline()
slash, line = readDir(file,slash,line)
When I run your code now I see the following error message:
I tracked the error to line 26 shown below:
If you take out the self parameter it should work. You only refer to self parameters within the class definition, not the rest of the program code. Hope this helps!
I think the self was in there because I was having problems with the init() function, as I posted in another topic.
It ended up resolving the issue with init() but caused the above bug apparently.