I've written code below to retrieve the current temperature from openweathermap , when the connection fails it should return the previous temperature.
Everything is working but i would like to avoid using the global variable last_temperature to save the previous retrieved temperature , how can I do this?
import time
from pyowm import OWM
API_key="**********************************"
owm=OWM(API_key)
last_temperature= 15.0
def getcurrentTemperature(city_id): # Gent is 2797656
global last_temperature
try:
obs = owm.weather_at_id(city_id) #by ID
w = obs.get_weather()
temps=w.get_temperature(unit='celsius')
last_temperature = round(temps['temp'],1)
return last_temperature
except:
return last_temperature
while 1 :
print getcurrentTemperature(2797656)
time.sleep(30)