Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

Listing 9.11 shows partial output from this Python script being run. The data being entered is record
high daily temperatures (Fahrenheit) for May in Indianapolis. After this data is entered, it is then
displayed back out to the screen.


LISTING 9.11 Output of script0901.py


Click here to view code image


pi@raspberrypi ~ $ python3 py3prog/script0901.py

Enter the record high temps (F) for May in Indianapolis...
Record high for May 1: 88
Record high for May 2: 85
Record high for May 3: 88
...
Record high for May 29: 90
Record high for May 30: 92
Record high for May 31: 90

Record High Temperatures (F) in Indianapolis during Race Month
May 1: 88
May 2: 85
May 3: 88
...
May 29: 90
May 30: 92
May 31: 90
pi@raspberrypi ~ $

Now your dictionary is loaded with record temperatures, in Fahrenheit. To convert the temperatures
to Celsius, you can make a small change to the original script.


Listing 9.12 shows part of the new script. The script user has to enter the Fahrenheit temperature data
only one time. Using a single loop on lines 11–17, the following happens to the temperature:


It is stored in a new dictionary for Fahrenheit temperatures.
It is converted to Celsius.
It is stored in another new dictionary for Celsius temperatures.

LISTING 9.12 The script0902.py Script


Click here to view code image


1: pi@raspberrypi ~ $ cat py3prog/script0902.py
2:
...
3: # Create the Celsius version of the dictionary
4: #
5: may_high_temp_c = {} #Create empty dictionary
6: #
7: may_high_temp_c.update(may_high_temp) #Create deep copy
8: #
9: date_keys = may_high_temp_c.keys() #Obtain list of element keys
10: #
Free download pdf