Python Forum
Getting "name 'get_weather' is not defined error and no json_data returned?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting "name 'get_weather' is not defined error and no json_data returned?
#3
I showed you a working function in your last Thread
Now you mess it up with serval errors one mention bye ibreeden and call json should be json_data.
All the globals values should not just get be used in function if there not are in function parameter.
As mention in last post do small teste as your function now it's not working at all,
then you stop and fix function before adding ConfigParser and GUI.

To show a example with ConfigParser using my code from last Thread.
As api_key is personal to you should not need to type in every time,have in api_key in the .ini file.
key.ini:
[key]
api_key = xxxxxxxxxxxxx
import requests
from configparser import ConfigParser

def get_weather(city, api_key):
    api = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
    json_data = requests.get(api).json()
    return json_data

def get_api_key():
    config = ConfigParser()
    config.read('key.ini')
    api_key = config['key']['api_key']
    return api_key

if __name__ == '__main__':
    city = 'Winnsboro,Louisiana'
    weather = get_weather(city, get_api_key())
    print(weather['name'])
    print(weather['main']['temp'])
    print(weather[ 'weather'][0]['description'])
Output:
Winnsboro 295.99 overcast clouds
Reply


Messages In This Thread
RE: Getting "name 'get_weather' is not defined error and no json_data returned? - by snippsat - Sep-13-2021, 12:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to access values returned from inquirer cspower 6 984 Dec-26-2023, 09:34 PM
Last Post: cspower
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 677 Nov-23-2023, 02:53 PM
Last Post: rob101
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,272 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,479 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  SQLAlchemy Object Missing when Null is returned Personne 1 1,823 Feb-19-2022, 02:50 AM
Last Post: Larz60+
  Libraries installed with pipenv, but ModuleNotFoundError returned jpncsu 2 3,108 Sep-06-2021, 07:24 PM
Last Post: jpncsu
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,488 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,271 Dec-14-2020, 05:45 PM
Last Post: karabakh
  name error "name"is not defined MaartenRo 1 3,508 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 2,354 Jun-13-2020, 02:48 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020