When I started my Weather Forecast I decided that I wanted to ask the user for location and display a graphic. I’ve now accomplished this, my program asks for Longitude and Latitude and now displays a picture depending on the weather – currently cold and wet here in Manchester.
——
#!/bin/python3
#geektechstuff
#print weather forecast
#imports required modules
import json, requests, turtle
#Variables
current_w2=”
LAT=”
LON=”
weather_img=r”
screen=”
print(“Weather Forecaster V3!”)
print(”)
LAT=input(“Please enter Latitude:”)
LON=input(“Please enter Longitude:”)
print(”)
#calls on the API of openweathermap.org
url=’http://api.openweathermap.org/data/2.5/forecast?lat=’+LAT+’&lon=’+LON+’&APPID=APIKIEY’
response=requests.get(url)
response.raise_for_status()
#Loads the JSON
weatherData=json.loads(response.text)
w=weatherData[‘list’]
#Uses the next forecast!
print(w[0][‘dt_txt’])
print(w[0][‘weather’][0][‘main’],’-‘,w[0][‘weather’][0][‘description’])
#Converts Kevlin into Celsius, as I’m more of a celsius person
temp_c=w[0][‘main’][‘temp’]
temp_c=float(temp_c)
temp_c=temp_c-273.15
print(‘Average temp in celsius:’,temp_c)
print(”)
#Checks the next temp
if temp_c <= 0:
print(‘WARNING: FREEZING POINT. Make sure home heating is on.’)
elif temp_c >18:
print(‘It\’s warm out today’)
#Bit more detail depending on the weather forecase
current_w=w[0][‘weather’][0][‘main’]
current_w2=current_w.lower()
if current_w2 == ‘snow’:
print(‘Snow; do you really need to go out?’)
weather_img=’snow.gif’
screen=turtle.Screen()
screen.setup(400, 400)
screen.bgpic(weather_img)
elif current_w2 == ‘rain’:
print(‘Raining cats and dogs’)
weather_img=’rain.gif’
screen=turtle.Screen()
screen.setup(400, 400)
screen.bgpic(weather_img)
print(”)
#Cycles through the next 10 3hr blocks which is roughly the next 30hrs
weather=range(1,10)
for x in weather:
print(w[x][‘dt_txt’])
print(w[x][‘weather’][0][‘main’],’-‘,w[x][‘weather’][0][‘description’])
#Converts Kevlin into Celsius, as I’m more of a celsius person
temp_c=w[x][‘main’][‘temp’]
temp_c=float(temp_c)
temp_c=temp_c-273.15
print(‘Average temp in celsius:’,temp_c)
x=x+1
print(”)
——