r/pythonhelp • • Feb 12 '23

Not working properly

Im trying to complete a project for my class and I can't get the displayWeather function to print properly. It should be triggered at the end of my Web_Data function, but nothing happens. I don't see any errors being reported, so i'm at a loss. I deleted my API key because I heard it was safer to do so when sharing code. But i have no clue because I'm so new to this. So an API key for openweathermap.org will be needed to run the code. Any help would be greatly appreciated.

# import required modules
import json
import requests

# display welcome message
print("Welcome to J.W. Weather Program.\n")


def Web_data():
    """Get zip code input for url"""

    base_url = "http://api.openweathermap.org/data/2.5/weather"
    apiKey = ""
    city = str(input("Please enter the city's zip code: "))

    url = f"{base_url}?q={city}&units=imperial&APPID={apiKey}"
    print(url)
    print()

    response = requests.get(url)
    data = response.json()
    displayWeather(data)


def displayWeather(data):
    temp = data["main"]["temp"]
    feel = data["mail"]["feels_like"]
    high = data['main']['temp_max']
    low = data['main']['temp_min']
    print(f"The temperature is: {temp} F.")
    print(f"It feels like: {feel}.")
    print(f"The high is: {high}.")
    print(f"The low is: {low}.")


def main():
    # Asks user if they want to search
    while True:
        resume = input("Do you want to know the weather? \n"
                       "Type 'Y' for yes or 'N' to quit: ")

        if resume == "y" or resume == "Y":
            try:
                Web_data()
            except Exception:
                print()
        if resume == "n" or resume == "N":
            break


main()
3 Upvotes

3 comments sorted by

1

u/Atmp Feb 12 '23

Is this a typo?

feel = data["mail"]["feels_like"] should be feel = data["main"]["feels_like"].

1

u/TryingToCodeDammit Feb 13 '23

yes. thank you

1

u/Odnan Feb 13 '23

It's possible that the displayWeather function is not being called at the end of Web_data function, or that the data being passed to the function is not in the expected format.

You can try adding a call to displayWeather at the end of Web_data function, passing it the data variable as an argument, like this:
def Web_data(:)
"""Get zip code input for url"""
base_url = "http://api.openweathermap.org/data/2.5/weather"
apiKey = "" # insert your API key here
city = str(input("Please enter the city's zip code: "))
url = f"{base_url}?q={city}&units=imperial&APPID={apiKey}"
print(url)
print()
response = requests.get(url)
data = response.json()
displayWeather(data # add this line)
def displayWeather(data:)
temp = data["main"]["temp"]
feel = data["main"]["feels_like"] # fix typo
high = data['main']['temp_max']
low = data['main']['temp_min']
print(f"The temperature is: {temp} F.")
print(f"It feels like: {feel}.")
print(f"The high is: {high}.")
print(f"The low is: {low}.")

If this doesn't work, try adding some print statements to help debug the code. For example, you can add a print statement at the beginning of the displayWeather function to check if it's being called.