Issue with returning custom object type

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

Repl link:

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)```

Hi @ThomasWarenski

You haven’t used classes correctly. I recommend you read up on these by using a site such as W3Schools or the Python website itself.

class directory():
   def __init__(self):
      self.name = ""
      self.contents = []
      self.sum = 0
1 Like

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)

Hi @ThomasWarenski I’m glad the previous reply helped!

When I run your code now I see the following error message:

image

I tracked the error to line 26 shown below:

image

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!

1 Like

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.

Hi @ThomasWarenski it would be easier if you kept one topic open for the Repl rather than posting multiple times.

The self should not be used outwith the class definition.

1 Like

Alright, I wasn’t sure because I’ve been on sites where they’re really picky that only one question goes in a topic.

Thanks @ThomasWarenski I understand but it was the same Repl, so easier to keep track asynchronously.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.