Hey all! I am looking for some help on the Hurricane Analysis project, part three particularly as I have been stuck after trying, googling, and searching the forums. Below I have attached my code that I am currently using. I have tried to workshop it, but I can’t figure out what I am doing wrong. Any insights are appreciated.
table = {}
def table(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths:
for items in range(len(names)):
table.update({names[items]: {months[items], years[items], max_sustained_winds[items],areas_affected[items], updated_damages[items], deaths[items]}})
print(table)
table(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths)
1 Like
Hi!
Right now, your goal is to create a dictionary where the key is the name of a hurricane, and the value is it’s corresponding hurricane dictionary.
Let’s look at your table.update
function. When you update your dictionary table
, you want to add the key (which you did!) and the value. Remember, the value is another dictionary, meaning each entry should have a key and a value. Right now, you only have the values for the corresponding dictionary! An easy fix is to just add strings for the values:
{"Month": months[items], "Year": years[items], " Wind Speed": max_sustained_winds[items], "Location": areas_affected[items], "Damages": updated_damages[items], "Deaths": deaths[items]}
P.S. It’s easier to get help when you format your code before posting! You can do this by putting backticks around your code
Read about it here: How do I format code in my posts?
1 Like
Hey! Thank you so much for your help and explanation. I really appreciate it, and not only has it helped me with the project, but with a better understanding as well.
Also for the formatting, thanks for the heads up. I didn’t know we could do that. It felt it a bit off posting the code as I did, and I appreciate knowing their is a better way going forward.
Update: (merged from duplicate topic by moderator)
I have been refining my code, but am stuck with a syntax error on the items within my table. I can not identify the issue. Any insights would be appreciated.
table = {}
def table(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths):
hurricanes = dict()
num_hurricanes = len(names)
for items in range(len(names)):
table[names[items]] = ("names": names[items],
"months": months[items], "years": years[items],
"max sustained winds": max_sustained_winds[items],
"areas affected: "areas_affected[items], "updated
damages": updated_damages[items], "deaths" deaths
[items])
1 Like
Hi again! 
I see you have made some changes! Remember, you’re creating a dictionary within a dictionary, aka dict = {key1: {dict1}, key2: {dict2}.....}
. Therefore, you still want to update your table
dictionary, just be sure the value for each key is it’s corresponding dictionary.
Also, since you are creating a function to update a dictionary, you should define the empty dictionary within the function and return it at the end. If you leave your code the way it is now, not only are you updating a table not defined within your function, but you are using the same name for your dictionary and your function. This is probably the cause of your syntax error.
Here’s an example of how you could do this (try to see if you can fix it before you look!):
def hurr_dict(nms, mnths, yrs, max_winds, areas, dmg, dths):
dict = {}
for i in range(len(names)):
dict.update({nms[i]: {"Name": nms[i], "Month": mnths[i], "Year": yrs[i], "Max Sustained Wind": max_winds[i], "Areas Affected": areas[i], "Damage": dmg[i], "Deaths": dths[i]}})
return dict
2 Likes
Hello Again!
Thank you again for your help. I was able to tune it up per your instructions and other resources, and then checked my work against yours. I ran my code and it worked, I’ve included it below for reference along with one other questiom.
def construct_hurr(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths):
table = {}
for items in range(len(names)):
table[names[items]] = {"Name": names[items],
"Month": months[items],
"Year": years[items], "Max Sustained Winds":
max_sustained_winds[items], "Areas Affected":
areas_affected[items], "Damages": updated_damages
[items], "Deaths": deaths[items]}
return table
Additionally , I have a question about my output. When I call my function it produces the following result:
‘Cuba I’: {‘Name’: ‘Cuba I’, ‘Month’: ‘October’, ‘Year’: 1924, ‘Max Sustained Winds’: 165, ‘Areas Affected’: [‘Central America’, ‘Mexico’, ‘Cuba’, ‘Florida’, ‘The Bahamas’], ‘Damages’: ‘Damages Not Recorded’, ‘Deaths’: 90},
From this, I cannot select keys within the dictionary, nor does it seem to be formatted correctly. Would you have advice on this?
Why not? Could you explain further?
1 Like
I’m not sure I entirely understand your question. Running your code, it seems to build the dictionary correctly.
If you are trying to select a key within the value dictionary (e.g., selecting Year
from the dictionary value assigned to Cuba I
), you would need to use the following code:
hurricane_records = construct_hurr(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths)
print(hurricane_records['Cuba I']['Year'])
# Output: 1924
When you are trying to reach a value that is in a dictionary within a dictionary, you have to start at the top level, which in the case above is hurricane_records
. Then you first select the key within that dictionary - we wanted the year of the Cuba I hurricane, so we look at the key 'Cuba I'
. We are now inside our Cuba I
and we want the year, so we look at the 'Year'
key, resulting in 1924.
I hope this helps! If this didn’t answer your question just let me know!
1 Like