I’m doing the Google course on Python, I’m a noob so apologies if it’s something obvious.
The task description says:
“The groups_per_user function receives a dictionary, which contains group names with the list of users. Users can belong to multiple groups. Fill in the blanks to return a dictionary with the users as keys and a list of their groups as values.”
Below is my latest attempt, the comments are from the course writers.
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for groups, users in group_dictionary.items():
# Now go through the users in the group
for user in users:
if user not in user_groups:
user_groups[user] += groups
# Now add the group to the the list of
# groups for this user, creating the entry
# in the dictionary if necessary
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }))
When I run it I get:
Error on line 18:
"administrator": ["admin"] }))
Error on line 8:
user_groups[user] += groups
KeyError: 'admin'
Any help would be appreciated, there isn’t a clear way to get detailed help from the course provider.
James is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.