Using a for loop to get the value in the dictionary

Viewed 51

Agnes decides that she wants to start creating targeted advertisements for people. Here is a list of customer objects with information about their name, age, job, pet, and pet name. You'll use loops to find people that meet certain requirements for Agnes' targeted marketing. Write for loops with conditional statements in conjunction with break and continue to get the desired output.

people = [
    {'name': "Daniel", 'age': 29, 'job': "Engineer", 'pet': "Cat", 'pet_name': "Gato"}, 
    {'name': "Katie", 'age': 30, 'job': "Teacher", 'pet': "Dog", 'pet_name': "Frank"},
    {'name': "Owen", 'age': 26, 'job': "Sales person", 'pet': "Cat", 'pet_name': "Cosmo"},
    {'name': "Josh", 'age': 22, 'job': "Student", 'pet': "Cat", 'pet_name': "Chat"},
    {'name': "Estelle", 'age': 35, 'job': "French Diplomat", 'pet': "Dog", 'pet_name': "Gabby"},
    {'name': "Gustav", 'age': 24, 'job': "Brewer", 'pet': "Dog", 'pet_name': "Helen"}
]

Use a for loop to find the first person in the list of people that has a dog as their pet. The iteration count shouldn't exceed 2. In your loop add a print statement that says

"{first_dog_person} has a dog! Had to check {number} of records to find a dog owner."

what i have done:

first_dog_person = None
iteration_count = 0
for person in people:
    iteration_count += 1
    if iteration_count == 2:
        break
    if person['pet'] == 'Dog':
        first_dog_person = person['name'] 
        print(f"{first_dog_person} has a dog! Had to check {iteration_count} of records to find a dog owner.")
        
       
    
print (f"{first_dog_person} has a dog! Had to check {iteration_count} of records to find a dog owner.")

even tho this still doesnt work I still need to use the continue function

1 Answers

The problem statement says that you shouldn't exceed 2 iterations. However, your code has this line:

    if iteration_count == 2:

This condition tests if you've reached the second iteration, not if you've exceeded it. Try stepping through your code using a debugger, paying special attention to this statement as you run through the iterations.

Related