help with classes

otc

Junior Member
Joined
Jan 16, 2022
Messages
106
Reaction score
30
hi all. i’m trying to understand classes but struggling. trying to make a simple class where it adds a name to a list then i can print it but nothing is printing can you guys help?

class Person:

List = []

def __init__(self, name):
self.name = name
self.List.append(name)

def printnames(self):
for i in range(len(self.List)):
print(self.List)


Ollie = Person("ollie")
John = Person("John")


Person.printnames
 
hi all. i’m trying to understand classes but struggling. trying to make a simple class where it adds a name to a list then i can print it but nothing is printing can you guys help?

class Person:

List = []

def __init__(self, name):
self.name = name
self.List.append(name)

def printnames(self):
for i in range(len(self.List)):
print(self.List)


Ollie = Person("ollie")
John = Person("John")


Person.printnames
class Person:

List = []

def __init__(self, name):
self.name = name
self.List.append(name)

def printnames(self):
for i in range(len(self.List)):
print(self.List)


Ollie = Person("ollie")
John = Person("John")



Person.printnames(John)

this works but idk why lol
 
class Person:

List = []

def __init__(self, name):
self.name = name
self.List.append(name)

def printnames(self):
for i in range(len(self.List)):
print(self.List)


Ollie = Person("ollie")
John = Person("John")



Person.printnames(John)

this works but idk why lol
class Person:

List = []

def __init__(self, name):
self.name = name
self.List.append(name)

def printnames():
for i in range(len(List)):
print(List)


Ollie = Person("ollie")
John = Person("John")



Person.printnames()

this would make more sense but it says
NameError: name 'List' is not defined
 
class Person:

List = []

def __init__(self, name):
self.name = name
self.List.append(name)

def printnames(self):
for i in range(len(self.List)):
print(self.List)


for i in range(1,5):
name_ = input("What is your name?\n")
name_ = Person(name_)




Person.printnames(name_)

this works, am i doing this inefficiently tho?
 
Back
Top