-1

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"])
  • Did you call `AddPerson` somewhere? – Robin Jan 26 '22 at 16:48
  • 1
    conditions come before `for`s in list comprehensions. [more info here](https://stackoverflow.com/questions/4260280/if-else-in-a-list-comprehension) btw make sure that `PeopleList` is not empty – Mehrdad Khojastefar Jan 26 '22 at 16:49
  • 1
    Your indentation is all messed up, please fix it – Random Davis Jan 26 '22 at 16:54
  • @MehrdadKhojastefar no that is not true. That is using a *conditional expression*, not a filtering condition, which comes *after* the `for` clause... that is a perfectly valid list comprehension. Or else it would throw a syntax error – juanpa.arrivillaga Jan 26 '22 at 16:57
  • @juanpa.arrivillaga it does not throw a syntax error because the `PeopleList` is empty so it does not get executed at all. although I'm pretty sure about the syntax but i test the code myself and let you know – Mehrdad Khojastefar Jan 26 '22 at 16:59
  • 1
    @MehrdadKhojastefar *syntax errors are raised at compile time*. It doesn't matter that the list comprehension is empty. And not to be too strident, but I am certain about this, but go ahead and test it out. But it is a *key feature* of a list comprehension, which allows you to combine mapping/filtering operations. You are confusing adding a *conditional expression* to the mapping part, so your link wouldn't *filter* the list, you can't just have `if condition` on the left of the `for` (the mapping part) – juanpa.arrivillaga Jan 26 '22 at 17:00
  • I agree with @RandomDavis, please fix the indentation of the code in your question — it's hard to tell what it does. Also, where is this list comprehension you're asking about? – martineau Jan 26 '22 at 17:08
  • 1
    @juanpa.arrivillaga yeah yo were right and it was very interesting .... this problem should be gone by just populating the `peopleList` – Mehrdad Khojastefar Jan 26 '22 at 17:11
  • thanks everyone! this really helps me study:) – Jonathan Kalmanovich Landau Jan 26 '22 at 19:19

1 Answers1

0

You didn't call the AddPerson function, so the list is empty, you should call somewhere

AddPerson() #call it when you want to add a person
XxJames07-
  • 1,833
  • 1
  • 4
  • 17