Hurricane Analysis Project Step 4

Hi there,
I’m stuck on step 4 and can’t figure out why my code results in assigning value only to last element. Please help!

Could you please link the project or explain a little (or both). It might not be obvious to a reader what step 4 actually is.

Are you supposed to modify an existing dictionary or create a new one, make sure you follow whatever instructions are given carefully there (I might be mistaken but writing a function to modify an existing dictionary like this seems a little wrong).

Your logical flow seems to be a little confused here. Maybe step back for a moment and write with pen and pencil or similar (instead of code) what should happen and when.

I’d highly suggest a different parameter name than dict as that masks the built-in dict() function (granted that’s only within the function but it’s awkward for anyone else who reads your code).

I think there also some slight confusion about the usage of if statements and else clauses.

You can’t separate if statements from additional clauses like an else with lines or expressions inbetween. For example the following would throw a syntax error (the if is a single statement, it cannot be broken up)-

x = 3 if x > 4: print("yes") y = 5 else: print("no")

In your example you don’t see a syntax error because for loops actually have their own else clauses- https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops. I don’t think that’s what you intended to use though so reconsider the flow here and make sure and else clauses belong to the right things.

1 Like

Sorry for missing this moment, here is the link to project:
https://www.codecademy.com/paths/data-analyst/tracks/dacp-python-fundamentals/modules/dscp-python-dictionaries-challenge-projects/projects/hurricane-analysis

In this project I have a dictionary that stores data on hurricanes. Name of hurricane is the key, whereas years in which occurred hurricane, affected areas, number of deaths etc. are dictionary values.
I stored years in the list and assigned them as the key for new dictionary in my function. After that I was trying to assign values from old dictionary to this new one (line of code above “return”), but it didn’t worked properly. It worked in way that only the last value from old dict was assigned to the last key in new one.

Actually I rewrote this function and now it works fine, but I’d like to know what was exact problem with those I attached above.

1 Like

Thanks for adding some background :+1:. I think you may have glossed over or missed some of the points I covered in that reply as I mentioned quite a few potential issues. Is there a part that isn’t clear?

In this second example you’ve removed a loop, an else clause and changed the type to a defaultdit, you must have had a reason to do so? I’d highly suggest solving this under your own steam if at all possible as it’s much more useful learning.

I was trying to find out how to store values in the list, so I used defaultdict. Now a see that it was unneсessary, so I removed it.

Thank you a lot for your advices, it was really helpful got it sorted! Now I see that in my first version of function the problem resided in improper usage of “append” method.