Working with variables

  • 1/19/2018

Weather snaps

Converting from Fahrenheit to centigrade is even more useful if you actually have some weather data to work from. The snaps function named get_weather_temp returns the temperature of a location in the United States. The information is provided by the US National Weather Service, www.weather.gov. You must supply the method with the latitude and longitude of the location for which you want the temperature. Here’s an example that gets the temperature for Seattle, Washington.

The latitude and longitude items in the call to the function are given as named arguments. You first saw examples of these in Chapter 3 when we gave named arguments to control the behavior of the snaps display_message function. Here we are giving two arguments that describe a location.

You can find the latitude of a town or city in the United States by using the Bing search engine. Just search for “Placename Latitude”, where Placename is where you live, and you’ll get the values you need to use.

If you want a brief description of the weather conditions at a location, you can use the get_weather_desciption method, which returns a short string describing the conditions at a given location.

# EG4-06 Seattle Weather

import snaps

desc=snaps.get_weather_desciption(latitude=47.61, longitude=-122.33)
print('The conditions are:',desc)

Keep in mind that these methods provide weather information only for locations in the United States. If you try to use latitude and longitude values for other countries or regions, the methods will cause your program to fail.