Abruptly Goblins: Step 13

Please reference the link below for the Abruptly Goblins Off-Platform project that I have a question about …
https://www.codecademy.com/courses/learn-python-3/informationals/python3-abruptly-goblins

My code for Step 10 (which is referenced in the Type Error message at the bottom of this post as Cell[113] …

def available_on_night(gamers_list, day):
    attending = []
    for gamer in gamers_list:
        if day in gamer['availability']:
            attending.append(gamer['name'])
    return attending
            
attending_game_night = available_on_night(gamers, game_night)
print(attending_game_night)

My code for Step 13 (which is referenced in the TypeError message near the bottom of this post as Cell[129] …

def not_available_on_night(gamers_list, day):
    attending = []
    not_attending = []
    for gamer in gamers_list:
        if day in gamer['availability']:
            attending.append(gamer['name'])
        else:
            not_attending.append(gamer['name'])
    return not_attending

unable_to_attend_best_night = not_available_on_night(gamers, game_night)
#print(unable_to_attend_best_night)

second_night_availability = build_daily_frequency_table()
calculate_availability(unable_to_attend_best_night, second_night_availability)
second_night = find_best_night(second_night_availability)

I received the following Type Error messages:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[129], line 15
     12 #print(unable_to_attend_best_night)
     14 second_night_availability = build_daily_frequency_table()
---> 15 calculate_availability(unable_to_attend_best_night, second_night_availability)
     16 second_night = find_best_night(second_night_availability)

Cell In[113], line 3, in calculate_availability(gamers_list, available_frequency)
      1 def calculate_availability(gamers_list, available_frequency):
      2     for gamer in gamers_list:
----> 3         for day in gamer['availability']:
      4             available_frequency[day] += 1

TypeError: string indices must be integers

I’ve gone back to double check and run my code in Step 10 (Cell[113]) and the output is the correct list of six gamer names. I know my code is not nearly as succinct and elegant as the provided Codecademy solution but I’d still like to learn if it is possible to make my code work. I’d appreciate it if someone can help me understand where I went wrong. I’ve been sitting on this problem for several days now …

Thank you.

The error is telling you that gamer is a string, and the index (value inside the square brackets) must be an integer. However, gamer should be a dictionary with a key named availability.

Working backwards from the error, you called calculate_availability with unable_to_attend_best_night as your first argument. In your function, gamers_list is assigned to that argument. Try printing gamers_list before your for loop. Is it a list of dictionaries?

Hint
def not_available_on_night(gamers_list, day):
    attending = []
    not_attending = []
    for gamer in gamers_list:
        if day in gamer['availability']:
            attending.append(gamer['name'])
        else:
            not_attending.append(gamer['name'])
    return not_attending

unable_to_attend_best_night = not_available_on_night(gamers, game_night)
#print(unable_to_attend_best_night)

When you printed unable_to_attend_best_night at the end of this snippet, you may have noticed that what you have here is a list of strings. It is only the names of the gamers. You need a list of dictionaries (the entire gamer not just the name).

2 Likes

Thanks midlindner for your response. After reading your explanation I saw the difference between my string solution and the necessary dictionary solution. I am relieved that all I had to do was take out the [‘name’] code in the line with the .append … instead of needing to unravel and reconstruct a whole new code. I think this worked. Thanks again.

1 Like