I have an arbitrary number of objects created from this class:
class Person:
def __init__(self, name, email):
self.name = name
self.email = email
I have a list of these objects:
myList = []
JohnDoe = Person("John Doe", "[email protected]")
BobbyMcfry = Person("Bobby Mcfry", "[email protected]")
WardWilkens = Person("Ward Wilkens", "[email protected]")
myList.append(JohnDoe)
myList.append(BobbyMcfry)
myList.append(WardWilkens)
I am wanting to check if someone exists, and if so, return their attributes - if not, say so:
x = input("Who to check for? ")
for i in myList:
if i.name == x:
print("Name: {0}\nEmail: {1}".format(i.name, i.email))
else:
print("{0} is not on the manifest.".format(x))
This kind of works, but returns one or the other for each Person in myList - I only want one return...
I realize I need to do some sort of
if val in myList:....
But I'm having trouble how to word what "val" should be without iterating through each object
breakand make your loop greedy and stop on the first match.