-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_app.py
30 lines (23 loc) · 894 Bytes
/
weather_app.py
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
# -*- coding: utf-8 -*-
"""Weather_app.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1U_cSOZICRgKjoETRHeqThWL4QuxjsCVY
"""
import requests
def get_weather(city, api_key):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
response = requests.get(url)
data = response.json()
if data["cod"] != "404":
main = data["weather"][0]["main"]
description = data["weather"][0]["description"]
temp = data["main"]["temp"] - 273.15
return f"Weather: {main},\nDescription: {description},\nTemperature: {temp:.2f}°C"
else:
return "City not found. Please try again."
if __name__ == "__main__":
api_key = "3c238d7e18db0f0702ba6167bbfb9b80"
city = input("Enter city name: ")
weather = get_weather(city, api_key)
print(weather)