• anonymous
Homework 3 - 3.4 Dictionaries, where is my error? I'm trying to solve the problem with a dictionary containing names and ages and I don't see why my code is wrong. def people(x): new = combine_lists(NAMES, AGES) #the function creates a dictionary for item in new: if new[item] == x: return item print people(18) For each name I check whether the value (=age) is say 18 or any other age and actually it should return all names with this age, but I only get one key even if there are more people with this age. What am I doing wrong? Thanks
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
  • Stacey Warren - Expert
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
  • katieb
I got my questions answered at in under 10 minutes. Go to now for free help!
  • rsmith6559
You're returning from people instead of accumulating, in say a list, your matches.
  • anonymous
I solved the problem using the function below. The program returns 'True' to all test cases, but it returns 'False' to the final test case, when it should return an empty list ! I'm not sure where i'm going wrong!? def people(n): """ takes a value and returns dictionary keys of that value""" di = (combine_lists(names,ages)) li=[] for na,a in di.iteritems(): if a == n: li.append(na) for look in li: return li print 'd' in people(18) and 'c' in people(18) print 'e' in people(19) and 'h' in people(19) and\ 'i'in people(19) and 'j' in people(19) and 'l' in people(19) print 'a' in people(20) and 'f' in people(20) and 'g'in people(20) print people(21) == ['b'] print people(22) == ['k'] print people(23) == []

Looking for something else?

Not the answer you are looking for? Search for more explanations.