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
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)-
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.
Sorry for missing this moment, here is the link to project:
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.
Thanks for adding some background . 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.
Howdy! I know you did this like ALLLLLONNNNNGGGG time ago.
Firstly this really helped me out, however there is only one issue with you code. If you where to try 2007, it would only return the first occurrence of 2007. This is because your loop only adds the data from the first time it sees the year and skips duplicates.
You need to add in a second if statement
if year in dic: dic[year].append(value)
This then will return the year with all the hurricanes in that year!
As I said, it was likely a long time ago that you did the course, hope you got a job or made your current job easier after doing the course.