Questo è il secondo post che metto in questo forum. Ho messo un post dove ho scritto che ho iniziato da poco a fare un mooc e fino ad oggi non ho avuto nessun problema. Il problema l'ho con i dizionari.
Ricevo questo errore:
[Executed at: Fri Apr 2 14:51:57 PDT 2021]
We found a few things wrong with your code. The first one is shown below, and the rest can be found in full_results.txt in the dropdown in the top left:
We tested your code with names = ["David Joyner", "David Zuber", "Brenton Joyner", "Brenton Zuber", "Nicol Barthel", "Shelba Barthel", "Shelba Crowley", "Shelba Fernald", "Shelba Odle", "Shelba Fry", "Maren Fry"].
We expected name_lists to return the dict
{'David': ['David Joyner', 'David Zuber'], 'Brenton': ['Brenton Joyner', 'Brenton Zuber'], 'Nicol': ['Nicol Barthel'], 'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald', 'Shelba Fry', 'Shelba Odle'], 'Maren': ['Maren Fry']}.
However, it returned the dict
{'David': ['David Joyner', 'David Zuber'], 'Brenton': ['Brenton Joyner', 'Brenton Zuber'], 'Nicol': ['Nicol Barthel'], 'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald', 'Shelba Odle', 'Shelba Fry'], 'Maren': ['Maren Fry']}.
Python:
#Recall in the previous problem you counted the number of
#instances of a certain first name in a list of full names.
#You returned a dictionary with the name as the key, and the
#number of times it appeared as the value.
#
#Modify that code such that instead of having a count as the
#value, you instead have a list of the full names that had
#that first name. So, each key in the dictionary would still
#be a first name, but the values would be lists of names.
#Make sure to sort the list of names, too.
#
#Name this new function name_lists.
def add_member_to_group(dictn, group_name, fullname):
#print("dic",group_name, fullname)
try:
dictn[group_name].append(fullname)
except KeyError:
dictn[group_name] = [fullname]
#this_group = dictn[group_name]
#this_group.append(fullname)
#Add your function here!
def name_lists(nlist):
# lista dei nomi
listname=[]
# dizionario con nomi completi + nomi
# dizionario return funzione con nome + lista
dictname={}
for name in nlist:
#print("crealista",name)
fname=name.split()
listname.append(fname[0])
#print("fname0",fname[0],name)
add_member_to_group(dictname,fname[0],name)
#if fname[0] in dictname:
# dictname[fname[0]].append(name)
#else:
# dictname[fname[0]]=name
#dictname[fname[0]]=name
#dictname.append(fname)
#print(fname," ",name)
#for name in listname:
#fname=name.split()
#listname.append(fname[0])
#print("creadic",name)
#if name in dictname:
# dictname[name]+=1
#else:
# dictname[name]=1
#print(dictname)
return dictname
#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print (although the order of the keys may vary):
#{'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald', 'Shelba Fry', 'Shelba Odle'],
#'David': ['David Joyner', 'David Zuber'], 'Brenton': ['Brenton Joyner', 'Brenton Zuber'],
#'Maren': ['Maren Fry'], 'Nicol': ['Nicol Barthel']}
name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
"Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
"Shelba Crowley", "Shelba Fernald", "Shelba Odle",
"Shelba Fry", "Maren Fry"]
print(name_lists(name_list))
Ricevo questo errore:
[Executed at: Fri Apr 2 14:51:57 PDT 2021]
We found a few things wrong with your code. The first one is shown below, and the rest can be found in full_results.txt in the dropdown in the top left:
We tested your code with names = ["David Joyner", "David Zuber", "Brenton Joyner", "Brenton Zuber", "Nicol Barthel", "Shelba Barthel", "Shelba Crowley", "Shelba Fernald", "Shelba Odle", "Shelba Fry", "Maren Fry"].
We expected name_lists to return the dict
{'David': ['David Joyner', 'David Zuber'], 'Brenton': ['Brenton Joyner', 'Brenton Zuber'], 'Nicol': ['Nicol Barthel'], 'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald', 'Shelba Fry', 'Shelba Odle'], 'Maren': ['Maren Fry']}.
However, it returned the dict
{'David': ['David Joyner', 'David Zuber'], 'Brenton': ['Brenton Joyner', 'Brenton Zuber'], 'Nicol': ['Nicol Barthel'], 'Shelba': ['Shelba Barthel', 'Shelba Crowley', 'Shelba Fernald', 'Shelba Odle', 'Shelba Fry'], 'Maren': ['Maren Fry']}.