View Single Post
Old May 14th, 2006, 7:45 AM   #22
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
That's because elif is just an extension of the original if statement (it's short for "else if"). If the first if statement succeeds, successive elif statements are not executed. Use "if" in place of "elif".

Also, don't use find. Use in! The in operator returns true if an element is in a list.
if tool in bob:
    owners.append('bob')
You might also wish to use a dictionary to store all of the data, rather than several lists. A dictionary matches keys to values. Consider the following dictionary:
people = dict(
    bob = ['hammer', 'saw'],
    joe = ['saw', 'drill', 'leveler'],
    bill = ['hammer', 'leveler']
)
This associates a set of strings with a set of lists. You can also write the same dictionary out in an alternative format:
people = {
    'bob' : ['hammer', 'saw'],
    'joe' : ['saw', 'drill', 'leveler'],
    'bill' : ['hammer', 'leveler']
}
You can use a dictionary to pull out information about a single person:
print "Bob's tools:", people['bob']
Or iterate through all the people:
for person, tools in people.items():
   print person, "has", tools
With a dictionary, finding out who owns what can be easily done with a list comprehension:
def whoowns(tool):
    return [person for person, tools in people.items() if tool in tools]
Arevos is offline   Reply With Quote