Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Lisa_Kramarenko

  • 3 years ago

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

  • This Question is Open
  1. rsmith6559
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You're returning from people instead of accumulating, in say a list, your matches.

  2. petew71
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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) == []

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy