Blossom - adding new flowers

Hello!

I’ve just done the Blossom project (Link), and as far as I can tell, I did it all correctly - it’s running without errors and it prints out the correct values for example keys at the end.

My Code

However, at the end it asks me to look up other flowers (I chose pansy) and how I would add them in, and I’m wondering how to do this? At first I thought, simple, just use the assign function. That didn’t work and I realised, well, it’s because the array has no more space (but we didn’t even implement a check for “nope, full!” so does that matter?), so I changed the instantiation to add one more. But it’s still printing out “None” as the value for “pansy”.
How do I add new flowers? (Other than adding them directly to flower_definitions?)

Edit: I’ve played around a bit with the size of the blossom instantiated hash map, and if I set it to 20 (as opposed to len(flower_definitions)), I get the correct values for daisy, sunflower and the manually added pansy. However, if I use values between 12 and 20, some or all of those start changing to None. I am so confused.

Edit 2: Found the problem (by checking everything against the walkthrough).
In the retrieve function, instead of

for item in list_at_index:
  if item[0] == key:
    return item[1]
  else:
    return None

I needed to do:

for item in list_at_index:
  if key == item[0]:
    return item[1]
return None

I’ll never get right what needs to go inside a loop and what doesn’t…

1 Like

Hi @textmaster61698,

Don’t be discouraged.

You were correct in removing this line from the loop:

return None

Do you understand why that line needs to follow the loop instead of being included within it?

1 Like

Hello, thanks for your reply.

I think I do, now.
So, in principle, I do understand that I don’t want it to iterate through the list and spit out “None” for every key that doesn’t match, as opposed to doing nothing if the key doesn’t match.

After a conversation with my partner (who knows very little Python but used to code in Basic back in the day), I’ve now figured out that the issue I had, and reason I’ve repeatedly made mistakes like this in other projects, is confusion over the use of “return”. Looking at this code:

for item in list_at_index:
  if key == item[0]:
    return item[1]
return None

I would have initially thought it would always return “None”, because “return None” isn’t subject to any if checks. Except it doesn’t, because it stops and, well, returns, if it does find the key in the loop and if check. But every time I’ve used return in Codeacademy lessons or examples so far, it was used for “spit out what comes after the return”, as opposed to “close off this block of code”. So I basically didn’t realise it also does that. In a writer’s ideal world, those would be two different commands.

1 Like

Note that a return statement does not act as a delimiter. It does not define the end of a block of statements. However, if it executes, it does terminate execution of that block by terminating execution of the containing function as a whole.

To clarify further, if, any only if, a particular return statement is executed, it terminates execution of the function that contains it, and passes a value back to the statement that called the function. If the return statement specifies an expression, that expression is evaluated, and the result of that evaluation is what is passed back. If no expression is specified, then None is passed back as the value.

Consistent with the above, only one return statement, at most, can execute during any particular call of the function that contains it.