Section #8 Solutions

Tracing Warmups

1. dict, dict, list, dict, str

2.

                for friend in data["network"]["Clinton"]["friends"]:
    if friend["name"] == "Anita":
        friend["years_friends"] = 7

Task 1: Find Missing Friends

            
                def find_missing_friends(data_dict):
    data = data_dict["network"]
    missing_friends = []
    for person in data:
        person_data = data[person]
        persons_friends = person_data["friends"]
        for friend in persons_friends:
            name = friend["name"]
            if name not in data and name not in missing_friends:
                print(name)
                missing_friends.append(name)
            
        

Task 2: Calculate Total Friendship Years

    
def calculate_total_years(data_dict, person):
    data = data_dict["network"]


    # Check if the person is in the network
    if person not in data:
        print(f"{person} is not in the network.")
        return


    # Calculate the total years of friendship
    total_years = 0
    person_dict = data[person]
    for friend in person_dict["friends"]:
        total_years += friend["years_friends"]


    print(f"{person} has a total of {total_years} years of friendship.")
    

Task 3: Make friends!

Shoutout Kevin for making this problem! Solutions here!