My homework needs to write functions in two classes(Person and World) and I'm pretty sure my code is correct. However,
"AttributeError: 'World' object has no attribute 'destination'"
keeps showing up when self.destination only exist in Person class.
It seems like the word 'self' is now referring to World class and I cannot figure out why.
class Person:
def __init__(self, world_size):
self.world_size = world_size
self.radius = 7
self.location = turtle.position()#this cause attribute error
self.destination = self._get_random_location()#and this causes too
#moves person towards the destination
def move(self):
turtle.setheading(turtle.towards(self.destination))
turtle.forward(self.radius/2)
Should I be replacing the 'self' with other words for Person class? If so, how could I do it?
class World:
def __init__(self, width, height, n):
self.size = (width, height)
self.hours = 0
self.people = []
self.add_person()
#everything involve of Person class in World class
#add a person to the list
def add_person(self):
person = Person(1)
self.people.append(person)
def simulate(self):
self.hours += 1
Person.update(self)
def draw(self):
p = Person(self)
p.draw()
**
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ \AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\ \AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\Desktop\VIRUS_PART_A.py", line 261, in __animation_loop
self.tick()
File "C:\Users\Desktop\VIRUS_PART_A.py", line 216, in next_turn
self.world.simulate()
File "C:\Users\Desktop\VIRUS_PART_A.py", line 124, in simulate
Person.update(self)
File "C:\Users\Desktop\VIRUS_PART_A.py", line 71, in update
Person.move(self)
File "C:\Users\Desktop\VIRUS_PART_A.py", line 79, in move
turtle.setheading(turtle.towards(self.destination))
AttributeError: 'World' object has no attribute 'destination'
**
You have Person on list self.people
so you should use this list in loop
def simulate(self):
self.hours += 1
for item in self.people:
item.update(self)
def draw(self):
for item in self.people:
item.draw()
Probably in Person
you use Person.move()
but you should use self.move()
or something similar.