-1

enter image description here

I need to return "None" for this but I know this is most simple with "get.dict" which wasn't working so I tried this approach. It works to return the correct values but will not return "none" for KeyError

  • Please post code here directly, not a screenshot. – Code-Apprentice Jul 18 '22 at 02:45
  • Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) … [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) …[Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – wwii Jul 18 '22 at 03:08

2 Answers2

0

You have the order switched. It should be:

stocks_dic.get(stock_price)
Zach Flanders
  • 1,224
  • 1
  • 7
  • 10
0

You are looking for dict.get and setting the default to None

stocks_dic.get(stock_price, None)

This will return None if the stock_price isn't in the dictionary instead of KeyError

For more information check out dict.get in the python docs

Alexander
  • 16,091
  • 5
  • 13
  • 29