I am writing a people cataloging program, and I want to search the class list using list comprehension, but the list does not recognize the x.fname
. what am I missing?
P.S I am studying python so any other tips are welcome!
This is my code:
class Person:
def __init__(self, fname, lname, ID, city, BD, parentID):
self.fname = fname
self.lname = lname
self.ID = ID
self.city = city
self.BD = BD
self.parentID = parentID
def printme (self):
print ("-----------------------Person profile------------------------")
print(f"""\nfirst name: {self.fname}\n
last name: {self.lname}\n
ID: {self.ID}\n
city: {self.city}\n
birth day: {self.BD}\n
parentID: {self.parentID}\n""")
def validInput(inp, inp_type, IDcheck):
if(inp_type == "string"):
i = input(f"enter {inp}:")
while (not i.replace(" ", "").replace("-", "").isalpha()):
i = input(f"ERROR, please enter {inp} again: ")
elif(inp_type=="int"):
i = input(f"enter {inp}: ")
while (not i.isdigit()):
i = input(f"ERROR, enter {inp} again: ")
if IDcheck == "y":
while (not len(i) == 3):
i = input (f"ERROR, enter {inp} again: ")
i = int(i)
else:
raise Exception("Error")
return i
PeopleList =[]
def AddPerson (lst):
fname = validInput("first name", "string", "n" )
lname = validInput("last name", "string", "n" )
ID = validInput("ID", "int", "y" )
city = validInput("city", "string", "n" )
parentID = validInput("Parent ID", "int", "y" )
p = Person(fname, lname, ID, city, "null", parentID)
PeopleList.append(p)
p.printme()
return PeopleList
p = Person("John", "len", 329, "New York", "07.09.2003" , 909)
p.printme()
print ([x for x in PeopleList if x.fname == "John"])