Skip to content

Commit e55477a

Browse files
Create weather-app.py
1 parent b534b4b commit e55477a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

weather-app.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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()

0 commit comments

Comments
 (0)