When trying to run the listPersons() command, every Person/Instance should call the sayHello() method. But as the names are str, it would raise an AttributeError (see below).
How do I format the names so I can use the methods on them?
class person:
def __init__ (self, name):
self.name = name
def sayHello(self):
print("Hello World, I'm", self.name)
def listPersons():
print ("There are", len(names), "persons here, please everybody say hello to the world!")
for name in names:
print(name.sayHello())
names = ["Tobias", "Lukas", "Alex", "Hannah"]
for name in names:
globals()[name] = person(name)
AttributeError:
Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
listPersons()
File "/Users/user/Desktop/test.py", line 12, in listPersons
print(name.sayHello())
AttributeError: 'str' object has no attribute 'sayHello'
Thank you so much for your help! :-)