You have a list of potential dates, where each person is represented by a dictionary…
- User
You have a list of potential dates, where each person is represented by a dictionary containing their name, gender, age, hobbies, and city. Here's a sample of what the list might look like:
potential_dates = [ { "name": "Sasha", "gender": "male", "age": 18, "hobbies": ["rock music", "art"], "city": "Berlin", }, { "name": "Maria", "gender": "female", "age": 35, "hobbies": ["art"], "city": "Berlin", }, { "name": "Daniel", "gender": "non-conforming", "age": 50, "hobbies": ["boxing", "reading", "art"], "city": "Berlin", }, { "name": "John", "gender": "male", "age": 41, "hobbies": ["reading", "alpinism", "museums"], "city": "Munich", }, ]
Complete the select_dates() function that takes the potential_dates list as an argument and returns a string containing the names of people who meet all of the following criteria:
Age is greater than 30
Interested in art (i.e., "art" is in their hobbies list)
Living in Berlin
The names in the returned string should be separated by commas and a space.
For the potential_dates list provided above, the expected output is
Maria, Daniel
Hint by avatar Lin M return ", ".join() Was this hint helpful? Write a program in Python 3 Report a typo AI Code Editor IDE100 ⌄ ⌄ ⌄ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 def select_dates(potential_dates): # List to store the names of people who meet the criteria selected_names = []
# Iterate through each person in the potential_dates list for person in potential_dates: # Check if the person meets all the criteria if person["age"] > 30 and "art" in person["hobbies"] and person["city"] == "Berlin": # If so, add their name to the selected_names list selected_names.append(person["name"]) # Join the names in the selected_names list with a comma and a space return ", ".join(selected_names)Call the function and print the result
print(select_dates(potential_dates))
Not correct, but keep on trying and never give up! 1 runtime error Let's try something else. Failed. Runtime error
Error: Traceback (most recent call last): File "/sandbox/main.py", line 16, in print(select_dates(potential_dates)) ^^^^^^^^^^^^^^^ NameError: name 'potential_dates' is not defined
- Assistant
The error you're encountering is due to the fact that the
potential_dateslist is not defined within the code you are running. To fix this, you need to ensure that thepotential_dateslist is provided before you call theselect_datesfunction. Here's how you can define the list and run the function:def select_dates(potential_dates): # List to store the names of people who meet the criteria selected_names = [] # Iterate through each person in the potential_dates list for person in potential_dates: # Check if the person meets all the criteria if person["age"] > 30 and "art" in person["hobbies"] and person["city"] == "Berlin": # If so, add their name to the selected_names list selected_names.append(person["name"]) # Join the names in the selected_names list with a comma and a space return ", ".join(selected_names) # Define the potential_dates list potential_dates = [ { "name": "Sasha", "gender": "male", "age": 18, "hobbies": ["rock music", "art"], "city": "Berlin", }, { "name": "Maria", "gender": "female", "age": 35, "hobbies": ["art"], "city": "Berlin", }, { "name": "Daniel", "gender": "non-conforming", "age": 50, "hobbies": ["boxing", "reading", "art"], "city": "Berlin", }, { "name": "John", "gender": "male", "age": 41, "hobbies": ["reading", "alpinism", "museums"], "city": "Munich", }, ] # Call the function and print the result print(select_dates(potential_dates))