Could you be more specific as to what " doesn’t work" means?
I see your answer stores the same instance variables and have the same method names, just that methods are called at different times. If your meaning of “doesn’t work” is that it fails answer checker, maybe the answer checker is checking for whether calc_length is called during __init__ and append
My problem was that i mistyped “fibonacci”, with this the code works :
class NewList():
def __init__(self,initial_state):
self.data=initial_state
def append(self,new_item):
self.data=self.data+[new_item]
def calc_lenght(self):
lenght=0
for item in self.data:
lenght+=1
return lenght
a_list=NewList([1,2,3])
print(a_list.data)
print(a_list.calc_lenght())
a_list.append(4)
print(a_list.data)
print(a_list.calc_lenght())
but still, why is the DataQuest answer so different ? :
# The NewList definition from the previous
# screen is copied here for your convenience
# class NewList(DQ):
# """
# A Python list with some extras!
# """
# def __init__(self, initial_state):
# self.data = initial_state
# def append(self, new_item):
# """
# Append `new_item` to the NewList
# """
# self.data = self.data + [new_item]
class NewList(DQ):
"""
A Python list with some extras!
"""
def __init__(self, initial_state):
self.data = initial_state
self.calc_length()
def calc_length(self):
"""
A helper function to calculate the .length
attribute.
"""
length = 0
for item in self.data:
length += 1
self.length = length
def append(self, new_item):
"""
Append `new_item` to the NewList
"""
self.data = self.data + [new_item]
self.calc_length()
fibonacci = NewList([1, 1, 2, 3, 5])
print(fibonacci.length)
fibonacci.append(8)
print(fibonacci.length)