forked from sumanth-0/100LinesOfPythonCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather Chatbot
More file actions
34 lines (31 loc) · 1.25 KB
/
Weather Chatbot
File metadata and controls
34 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# pip install requests
# code for weather information chatbot
import requests
class WeatherChatbot:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "http://api.openweathermap.org/data/2.5/weather"
def get_weather(self, city):
url = f"{self.base_url}?q={city}&appid={self.api_key}&units=metric"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
weather_description = data['weather'][0]['description']
temperature = data['main']['temp']
return f"The weather in {city} is currently {weather_description} with a temperature of {temperature}°C."
else:
return "Sorry, I couldn't find the weather information for that city."
def main():
api_key = "YOUR_API_KEY" # Replace with your OpenWeatherMap API key
bot = WeatherChatbot(api_key)
print("Welcome to the Weather Chatbot!")
print("You can ask for the weather in any city. Type 'exit' to quit.")
while True:
city = input("Enter a city: ")
if city.lower() == 'exit':
print("Goodbye!")
break
weather_info = bot.get_weather(city)
print(weather_info)
if __name__ == "__main__":
main()