Shengjing Shengjing - 6 months ago 10
Python Question

Why is the if clause being skipped?

# in file contactList.py
import contact
class ContactList:

def __init__(self):

self.added = True
self.list = []

def add(self, newContact):

added = 1
self.list.append(newContact)

def __str__(self):

if (self.added == True):
returnedString = ""
for contact in self.list:
returnedString = returnedString + "\n" + str(contact)
return returnedString

def find(self, name):

self.name = name
for contact in self.list:
if self.name == (str(contact))[0]: # I also tried if self.name in str(contact)
return str(contact)
else:
return "Invalid input"


if __name__ == "__main__":
myFriends = ContactList()
friend1 = contact.Contact("Mickey", "650-345-3333", "Mickey@disneyland.com", "Disneyland, California")
friend2 = contact.Contact("Minnie", "650-345-3344", "Minnie@disneyworld.com", "Disneyworld, Florida")
friend3 = contact.Contact("Donald", "650-345-3333", "Donald@EuroDisney.com", "EuroDisney, France")
myFriends.add(friend1)
myFriends.add(friend2)
myFriends.add(friend3)
print (myFriends)

print (myFriends.find("Mickey"))


I'm wondering why the last call,
print (myFriends.find("Mickey"))
, is not be iterated by the shell; instead, it's skipped no matter what I put in the parameter. For example, when I enter "Mickey", "Minnie", or "Donald" in the parameter, I'm supposed to get the corresponding personal information about them, but the "if" clause under the
find()
function is never iterated. What's the problem here? Besides, since it was a snippet code to which I added
find()
, I'm not really sure what the purpose of the
self.added = True
in
__init__
and the
added = 1
in
add()
.

Below is the
contact()
imported to this file
contactList.py


#in file contact.py
class Contact:

"""
One object of class Contact represents one person's contact info.
"""
def __init__(self, name, phone, email, streetAddress):
self.name = name
self.phone = phone
self.email = email
self.streetAddress = streetAddress

def __str__(self):
return "%s\n%s\n%s\n%s\n" % (self.name, self.phone,self.email,self.streetAddress)

if __name__ == "__main__":
friend1 = Contact("Mickey", "650-345-3333", "Mickey@disneyland.com", "disneyland, CA")
print (friend1)

Answer

Your code as it is written will only check the first contact in the list. I'll just repeat what you have written here for clarity:

def find(self, name):
    self.name = name
    for contact in self.list:
        if self.name == (str(contact))[0]:
            return str(contact)
        else:
            return "Invalid input"

Now to be extra clear let's just keep the important parts:

if :# some condition is true
    return str(contact)
else: # some condition is false
    return "Invalid input"

As you can see in either case you get a return value. So you're not going to be finding your value if it's not the first one in your list, doesn't matter if the "some condition" referred to above is correct or buggy.

Edit: to be crystal clear, you need to put your failure condition (returning "invalid input" or whatever) after the for loop, not inside of it. This is left as an exercise to the reader.

Now as for the condition you're checking, based on the structure of the Contact class you posted I'd guess the right check would be:

if name == contact.name:

Nice and neat like python should be :)

Good luck

Comments