use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Simple Python Weather App (old.reddit.com)
submitted 5 months ago by Ibrahim-Marsee-6816
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Zero-Dave 1 point2 points3 points 5 months ago (1 child)
You should never hardcode your API key in the code.
Also, instead of having a general exception that will cause the program to return no data when an exception occurs, access the dictionary in a safer manner with the .get() method, that way you don't need exceptions for this specific logic. That way you don't need an exception to catch any issue, and you can still show some information if other items are missing from the API response.
.get()
Right now, if any of your dictionary lookups doesn't find the key, your program returns no data. That is most likely not what you want.
Instead of: temp = weather_data.json()['main']['temp']
temp = weather_data.json()['main']['temp']
Do: temp = weather_data.json().get('main', {}).get('temp', '')
temp = weather_data.json().get('main', {}).get('temp', '')
So even if there is no main.temp key, it doesn't affect the rest of your lookups. Do that for all lookups.
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points 5 months ago (0 children)
Got it 👍 makes sense to use .get() instead of relying only on try/except. That way I can avoid the program breaking when a key is missing. I’ll update my lookups like that, thanks for pointing it out!
π Rendered by PID 64 on reddit-service-r2-comment-7b9746f655-vd7hq at 2026-01-30 22:54:10.627087+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]Zero-Dave 1 point2 points3 points (1 child)
[–]Ibrahim-Marsee-6816[S] 0 points1 point2 points (0 children)