File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ def get_temperature (city_name , api_key ):
4
+ base_url = "https://api.openweathermap.org/data/2.5/weather"
5
+ params = {
6
+ 'q' : f"{ city_name } ,US" ,
7
+ 'appid' : api_key ,
8
+ 'units' : 'imperial' # Fahrenheit
9
+ }
10
+ response = requests .get (base_url , params = params )
11
+
12
+ if response .status_code == 200 :
13
+ data = response .json ()
14
+ temp = data ['main' ]['temp' ]
15
+ return temp
16
+ else :
17
+ return None
18
+
19
+ def main ():
20
+ api_key = "YOUR_API_KEY_HERE" # Replace this with your OpenWeatherMap API key
21
+ city = input ("Enter a major U.S. city: " ).strip ()
22
+
23
+ temp = get_temperature (city , api_key )
24
+ if temp is not None :
25
+ print (f"The current temperature in { city .title ()} is { temp :.1f} °F." )
26
+ else :
27
+ print ("Sorry, couldn't find weather data for that city." )
28
+
29
+ if __name__ == "__main__" :
30
+ main ()
You can’t perform that action at this time.
0 commit comments