I am creating a basic chatbot that can currently tell you the current weather conditions and temperature. However, when I ask it to tell me the temperature or weather conditions, it doesn't givea any output at all, not even an exception. Here is my code:
#This is the main.py
import numpy as np
import getweather as gw
from getweather import get_weather_forecast, get_current_temp
import tracemalloc
import asyncio
weather_cmds = np.array(["What is the weather like?", "What is the current weather?", "What is the weather right now?"])
temp_qs = np.array(["What is the temperature today?", "Is it hot or cold right now?", "What is the temperature like?", "What is the temperature right now?"])
greetings = np.array(["Hello.", "Hi!", "Hey!", "Hey there!"])
questions = np.array(["How can I help?", "What can I do for you?", "What do you need help with?"])
Hellos = np.concatenate((greetings, questions))
greeting = np.random.choice(greetings)
question = np.random.choice(questions)
task = input(f"{greeting} {question}\n>> ")
if task in weather_cmds:
location = input("Enter country: ")
tracemalloc.start()
print(asyncio.run(get_weather_forecast(location)))
tracemalloc.stop()
elif task in temp_qs:
if task in temp_qs[0] or task in temp_qs[3]:
location = input("Enter country: ")
tracemalloc.start()
print(asyncio.run(get_current_temp(location)))
tracemalloc.stop()
#this is the getweather module
import python_weather
import tracemalloc
import random
async def get_weather_forecast(location):
async with python_weather.Client() as client:
try:
weather = await client.get(location)
weather_descrip = weather.current.description.lower()
return f"In {location}, it is {weather.current.temperature}°C and {weather_descrip}."
except python_weather.exceptions.BadApiKeyError:
return "API key is invalid."
except python_weather.exceptions.NotFoundError:
return f"Could not find weather information for {location}."
async def get_current_temp(location):
async with python_weather.Client() as client:
weather = await client.get(location)
return f"The temperature is {weather.current.temperature}°C in {location}."
I have checked through all of my print statements, they seem fine, and I have reinstalled python_weather expecting the issue to be solved but nothing worked and there wasn't any output whatsoever.
I found a way, after lots of debugging and modification...
So I started by debugging and I found that the connection was successful, but it wasn't running any return or print() lines of code.
As It turns out, there was a new update to python_weather
which no longer supports the units argument, so I removed it and it works now!
This is the correct code:
import python_weather
import random
import asyncio
async def get_weather_forecast(location):
async with python_weather.Client() as client:
try:
weather = await client.get(location)
weather_descrip=weather.current.description.lower()
return f"In {location}, it is {weather.current.temperature}°C and {weather_descrip}."
except python_weather.exceptions.BadApiKeyError:
return "API key is invalid"
except python_weather.exceptions.NotFoundError:
return f"Could not find weather information for {location}."
async def get_current_temp(location):
async with python_weather.Client() as client:
weather=await client.get(location)
responses = [
f"In {location}, it is {weather.current.temperature}°C",
f"The current temperature in {location} is {weather.current.temperature}°C",
f"{weather.current.temperature}°C is the current temperature in {location}"
]
return random.choice(responses)