Difference between revisions of "CSC111 Python Programs Fall 2015"

From dftwiki3
Jump to: navigation, search
(Nov. 30, 2015)
(Nov. 30, 2015)
Line 2,454: Line 2,454:
 
==Cambridge.data==
 
==Cambridge.data==
 
<br />
 
<br />
::<source lang="text">
+
* You can get the data from this URL: [http://cs.smith.edu/~dthiebaut/UKTemperatures/cambridgedata.txt http://cs.smith.edu/~dthiebaut/UKTemperatures/cambridgedata.txt]
Cambridge NIAB
 
Location: 5435E 2606N, 26 metres amsl
 
Estimated data is marked with a * after the value.
 
Missing data (more than 2 days missing in month) is marked by  ---.
 
Sunshine data taken from an automatic Kipp & Zonen sensor marked with a #, otherwise sunshine data taken from a Campbell Stokes recorder.
 
  yyyy  mm  tmax    tmin      af    rain    sun
 
              degC    degC    days      mm  hours
 
  1959  1    4.4    -1.4      20    ---    78.1
 
  1959  2    7.5    1.2      9    ---    66.0
 
  1959  3  11.5    3.8      0    ---    98.0
 
  1959  4  14.3    5.4      0    ---  146.1
 
  1959  5  18.1    6.5      0    ---  224.8
 
  1959  6  21.6    10.1      0    ---  252.4
 
  1959  7  23.9    12.2      0    ---  270.7
 
  1959  8  23.5    13.0      0    ---  213.4
 
  1959  9  21.7    9.0      0    ---  171.3
 
  1959  10  18.0    7.5      0    ---  167.6
 
  1959  11  10.2    3.2      6    ---    59.9
 
  1959  12    8.7    3.2      2    ---    36.7
 
  1960  1    6.7    1.6      12    ---    36.3
 
  1960  2    7.5    1.4      9    ---    72.7
 
  1960  3    9.7    2.9      5    ---    61.6
 
  1960  4  14.0    4.5      2    ---  162.7
 
  1960  5  18.5    8.2      0    ---  202.6
 
  1960  6  21.7    10.4      0    ---  260.2
 
  1960  7  20.1    11.4      0    ---  154.7
 
  1960  8  20.6    10.9      0    ---  159.8
 
  1960  9  17.7    9.5      0    ---  125.0
 
  1960  10  14.0    7.6      0    ---    71.8
 
  1960  11  10.6    4.6      0    ---    65.4
 
  1960  12    6.5    2.1      5    ---    48.6
 
  1961  1    6.2    1.0      5    55.9    50.5
 
  1961  2  10.3    4.3      0    50.4    65.9
 
  1961  3  13.7    3.3      1    5.2  170.4
 
  1961  4  14.9    6.2      0    44.5  101.0
 
  1961  5  16.4    6.4      0    23.6  218.4
 
  1961  6  21.4    9.8      0    32.7  243.2
 
  1961  7  20.9    11.3      0    32.8  185.6
 
  1961  8  21.3    11.6      0    50.3  182.6
 
  1961  9  20.4    11.5      0    63.8  131.1
 
  1961  10  15.3    7.3      0    57.8  141.4
 
  1961  11    9.4    2.9      6    41.5    72.7
 
  1961  12    5.5    -0.6      18    79.7    58.3
 
  1962  1    7.3    0.7      9    62.3    73.5
 
  1962  2    7.6    1.2      9    11.4    79.3
 
  1962  3    6.8    -1.4      17    26.5  102.4
 
  1962  4  12.3    4.1      1    38.8  139.5
 
  1962  5  14.5    6.6      1    45.9  160.1
 
  1962  6  19.6    8.0      0    0.5  289.6
 
  1962  7  19.4    11.0      0    56.0  126.4
 
  1962  8  19.9    11.0      0    55.1  182.1
 
  1962  9  17.5    8.9      0    73.6  129.6
 
  1962  10  14.9    6.5      1    32.5  104.4
 
  1962  11    8.2    2.6      8    35.5    34.2
 
  1962  12    4.6    -2.5      18    33.7    68.6
 
  1963  1  -0.1    -5.7      29    24.7    58.0
 
  1963  2    1.2    -3.5      27    13.2    59.0
 
  1963  3    9.9    2.8      7    50.3  102.6
 
  1963  4  13.4    4.7      3    41.0  124.9
 
  1963  5  15.8    6.3      0    43.5  189.5
 
  1963  6  20.6    10.0      0    43.0  201.8
 
  1963  7  20.6    10.6      0    52.4  170.1
 
  1963  8  19.0    10.6      0  100.4  119.6
 
  1963  9  17.9    9.3      0    44.1  134.0
 
  1963  10  14.3    8.0      0    48.6    78.4
 
  1963  11  11.4    5.2      1    78.6    52.8
 
  1963  12    5.0    -0.7      20    11.1    48.7
 
  1964  1    5.0    -0.3      15    18.7    45.8
 
  1964  2    7.4    1.8      8    16.6    70.8
 
  1964  3    6.7    1.5      10    76.2    52.4
 
  1964  4  12.9    5.1      2    59.1  113.9
 
  1964  5  19.6    8.7      0    21.5  208.5
 
  1964  6  18.7    10.4      0    94.7  150.7
 
  1964  7  22.2    12.2      0    40.2  198.0
 
  1964  8  21.6    11.5      0    21.8  200.2
 
  1964  9  20.5    8.9      0    12.1  196.1
 
  1964  10  13.3    4.6      0    27.1  114.2
 
  1964  11  10.5    4.0      6    34.8    63.0
 
  1964  12    6.5    0.1      17    37.3    48.7
 
  1965  1    6.1    0.9      16    47.4    78.3
 
  1965  2    5.6    0.6      10    15.1    29.1
 
  1965  3  10.0    1.1      11    51.8  125.0
 
  1965  4  12.9    4.2      2    42.4  122.2
 
  1965  5  16.5    7.8      0    47.8  166.2
 
  1965  6  19.2    9.7      0    43.2  174.8
 
  1965  7  18.3    10.4      0    63.6  103.4
 
  1965  8  20.3    10.2      0    65.4  174.0
 
  1965  9  17.5    8.8      0    85.6  127.0
 
  1965  10  15.4    6.1      2    14.8  115.1
 
  1965  11    8.0    1.6      14    36.9    86.5
 
  1965  12    7.3    1.4      8    81.7    52.0
 
  1966  1    4.6    0.4      16    29.6    34.0
 
  1966  2    8.8    3.3      8    56.7    43.6
 
  1966  3  10.8    2.6      7    8.5  115.9
 
  1966  4  11.6    4.5      2    65.4    82.2
 
  1966  5  16.7    6.5      0    35.8  196.7
 
  1966  6  21.5    10.8      0    56.9  181.6
 
  1966  7  19.5    11.1      0    70.4  145.5
 
  1966  8  20.4    10.6      0    75.0  186.6
 
  1966  9  19.0    10.1      0    35.0  148.3
 
  1966  10  14.4    7.6      0    64.7    74.9
 
  1966  11    8.3    2.7      5    39.9    52.2
 
  1966  12    8.4    2.0      7    69.6    39.9
 
  1967  1    7.1    2.0      8    21.8    71.9
 
  1967  2    8.9    2.0      7    34.7    88.3
 
  1967  3  11.7    4.0      0    18.9  188.3
 
  1967  4  12.0    4.0      3    53.1  134.7*
 
  1967  5  15.5    6.9      1  118.0  178.6
 
  1967  6  19.5    9.2      0    29.7  213.6
 
  1967  7  23.3    12.9      0    61.4  211.0
 
  1967  8  21.4    11.6      0    22.1  152.8
 
  1967  9  18.1    10.0      0    39.1  110.3
 
  1967  10  14.6    8.2      0  107.1    94.9
 
  1967  11    8.6    2.2      8    44.9    59.4
 
  1967  12    6.4    1.0      13    40.8    65.4
 
  1968  1    6.8    0.9      13    37.2    38.2
 
  1968  2    4.8    -0.5      19    17.2    40.5
 
  1968  3  11.1    2.9      5    10.1  130.8
 
  1968  4  13.6    3.7      8    36.8  172.0
 
  1968  5  15.0    6.0      0    32.7  149.4
 
  1968  6  20.1    10.3      0    75.9  172.4
 
  1968  7  19.5    11.3      0    83.5  127.6
 
  1968  8  19.6    11.7      0    93.8  120.8
 
  1968  9  18.4    10.7      0    85.7  118.8
 
  1968  10  16.1    10.1      0    42.1    64.0
 
  1968  11    9.0    4.3      5    30.9    28.7
 
  1968  12    4.8    0.8      10    39.8    18.8
 
  1969  1    8.4    3.4      6    58.9    36.1
 
  1969  2    3.5    -1.6      16    41.9    76.3
 
  1969  3    6.6    0.8      6    53.9    66.4
 
  1969  4  13.1    2.8      6    32.0  190.0
 
  1969  5  16.4    8.0      0    90.7  166.0
 
  1969  6  19.4    8.6      0    30.3  268.7
 
  1969  7  22.9    12.5      0    44.1  213.9
 
  1969  8  21.0    12.0      0    59.6  119.8
 
  1969  9  18.8    10.6      1    2.4  123.4
 
  1969  10  17.8    9.1      0    4.6  100.0
 
  1969  11    8.9    2.7      5    64.3    64.0
 
  1969  12    5.3    0.7      12    49.8    19.2
 
  1970  1    6.3    1.4      10    52.4    30.3
 
  1970  2    6.3    0.1      11    55.9  112.9
 
  1970  3    7.1    -0.5      16    32.3  109.0
 
  1970  4  10.8    3.2      5    66.4  125.4
 
  1970  5  18.6    8.4      0    19.6  213.3
 
  1970  6  22.5    10.7      0    96.5  264.8
 
  1970  7  20.4    11.5      0    33.1  184.3
 
  1970  8  21.5    11.1      0    33.2  181.0
 
  1970  9  19.9    10.6      0    44.2  161.3
 
  1970  10  15.5    7.3      0    20.3  105.8
 
  1970  11  11.0*    4.9      2  117.8    60.0
 
  1970  12    6.6    1.5      12    34.6    37.8
 
  1971  1    7.1    2.0      9    71.6    39.6
 
  1971  2    7.9    1.5      7    11.8    76.6
 
  1971  3    8.4    1.6      9    41.2  101.8
 
  1971  4  11.5    4.0      2    30.3  119.6
 
  1971  5  17.2    6.3      2    51.9  239.8
 
  1971  6  16.9    9.1      0    96.4  131.0
 
  1971  7  22.5    12.2      0    16.3  238.3
 
  1971  8  20.8    12.2      0    57.4  147.2
 
  1971  9  20.1    8.5      0    21.4  173.1
 
  1971  10  16.5    5.7      1    65.0  150.4
 
  1971  11    9.5    1.7      11    61.9    96.4
 
  1971  12    8.9    3.7      2    17.0    39.5
 
  1972  1    6.0    1.6      8    46.0    48.6
 
  1972  2    7.5    1.6      5    32.7    46.1
 
  1972  3  11.8    2.0      5    42.1  150.0
 
  1972  4  12.1    5.1      0    43.9  136.1
 
  1972  5  15.7    6.8      0    26.0  205.2
 
  1972  6  17.4    8.1      0    22.7  190.0
 
  1972  7  20.9    11.4      0    26.8  167.0
 
  1972  8  20.8    10.9      0    37.6  189.1
 
  1972  9  16.4    8.1      0    29.9  121.4
 
  1972  10  15.3    6.5      1    7.8  108.1
 
  1972  11    9.6    3.2      7    48.9    83.3
 
  1972  12    8.6    2.6      2    39.0    49.8
 
  1973  1    6.9    1.8      8    14.8    39.7
 
  1973  2    7.8    0.9      11    19.8    88.5
 
  1973  3  11.1    2.0      4    10.4  134.4
 
  1973  4  12.0    3.2      3    42.5  166.0
 
  1973  5  16.7    7.2      0    66.5  200.8
 
  1973  6  21.2    9.7      0    60.6*  273.1
 
  1973  7  20.9    11.5      0    36.4  165.5
 
  1973  8  23.1    11.9      0    18.4  198.1
 
  1973  9  20.1    10.2      0    57.5  170.5
 
  1973  10  13.3    5.2      2    25.9  110.4
 
  1973  11    9.5    1.7      10    30.1  104.9
 
  1973  12    8.0    1.8      8    29.5    57.3
 
  1974  1    9.2*    2.7      3    46.2    66.4
 
  1974  2    8.6    2.7      5    47.7    73.7
 
  1974  3    9.6    2.6      3    16.2  100.8
 
  1974  4  13.4    2.9      3    10.3  152.8
 
  1974  5  16.2    6.1      0    22.3  226.0
 
  1974  6  19.3    9.5      0    52.8  209.3
 
  1974  7  20.1    11.1      0    29.1  215.3
 
  1974  8  20.8    11.0      0    89.9  206.4
 
  1974  9  16.9    8.7      0    71.2  171.4
 
  1974  10  10.7    4.7      1    88.2    96.8
 
  1974  11    9.8*    4.1      1  126.1    58.3
 
  1974  12  10.6    5.7      0    28.3    52.0
 
  1975  1  10.1    3.9      3    51.9    38.0
 
  1975  2    8.3    1.2      10    25.2    53.7
 
  1975  3    7.8    2.3      7    92.4    70.8
 
  1975  4  12.6    4.6      5    79.7  128.6
 
  1975  5  14.4    5.9      0    48.5  180.6
 
  1975  6  20.8    8.8      0    18.0  282.3
 
  1975  7  23.4    12.6      0    27.4  222.0
 
  1975  8  25.3    13.5      0    26.2  234.3
 
  1975  9  19.4    9.4      0    69.8  162.5
 
  1975  10  14.2    6.3      0    6.4  126.6
 
  1975  11    9.1    2.4      9    37.6    70.6
 
  1975  12    7.4    1.7      10    28.1    30.5
 
  1976  1    8.3    3.1      9    29.2    52.9
 
  1976  2    7.2    1.5      9    14.4    53.5
 
  1976  3    9.0    0.9      12    15.7  116.0
 
  1976  4  12.9    3.9      4    21.4  175.0
 
  1976  5  17.9    7.6      0    31.1  197.8
 
  1976  6  24.5    11.4      0    8.3  277.8
 
  1976  7  25.7    12.4      0    36.2  282.2
 
  1976  8  24.2    10.7      0    33.2  238.2
 
  1976  9  18.1    9.7      0    72.7  117.1
 
  1976  10  14.4    8.2      0    85.2    57.3
 
  1976  11    9.3    3.4      2    76.3    71.9
 
  1976  12    4.5    -0.4      13    74.6    61.2
 
  1977  1    5.3    0.6      12    45.7    43.5
 
  1977  2    9.0    2.6      3    64.7    65.1
 
  1977  3  11.0    3.6      5    33.7    98.8
 
  1977  4  12.2    3.3      8    29.2  173.5
 
  1977  5  15.7    5.5      0    39.9  236.8
 
  1977  6  16.6    7.3      0    53.9  138.5
 
  1977  7  21.3    10.2      0    4.7  208.7
 
  1977  8  20.0    10.1      0  128.0  149.9
 
  1977  9  18.1    9.3      0    13.7  126.5
 
  1977  10  16.2    8.0      0    21.2  120.0
 
  1977  11    9.6    3.7      4    48.3    86.9
 
  1977  12    9.0    3.9      3    51.6    48.8
 
  1978  1    5.9    -0.7      15    61.5    39.6
 
  1978  2    5.4    0.1      14    39.0    54.4
 
  1978  3  11.1    3.2      2    58.9  116.1
 
  1978  4  10.3    2.6      4    44.7  103.2
 
  1978  5  16.0    6.1      0    77.9  187.4
 
  1978  6  18.9    9.3      0    45.9  154.8
 
  1978  7  19.4    10.4      0    36.9  112.7
 
  1978  8  20.2    11.1      0    32.8  137.1
 
  1978  9  19.3    10.8      0    40.2  163.1
 
  1978  10  16.3    8.6      0    5.2    98.7
 
  1978  11  11.7    5.0      5    13.7    84.9
 
  1978  12    6.7    1.6      10    99.3    37.2
 
  1979  1    2.5    -3.0      23    42.8    56.4
 
  1979  2    3.5    -1.3      19    46.3    44.0
 
  1979  3    8.4    1.8      3    84.2    94.1
 
  1979  4  12.1    4.2      1    33.2  113.8
 
  1979  5  15.9    6.6      1    94.6  201.1
 
  1979  6  18.9    9.8      0    13.3  177.8
 
  1979  7  21.8    11.8      0    11.9  178.2
 
  1979  8  20.4    11.0      0    74.7  170.7
 
  1979  9  19.3    9.6      0    18.8  181.0
 
  1979  10  15.5    7.2      0    61.4  120.0
 
  1979  11  10.0    3.2      3    43.1    68.1
 
  1979  12    8.8    3.3      4  104.0    62.4
 
  1980  1    5.6    -0.2      12    33.7    65.0
 
  1980  2    9.1    2.6      6    42.9    49.1
 
  1980  3    8.5    1.8      7    57.5    80.5
 
  1980  4  13.3    4.0      2    17.9  160.8
 
  1980  5  16.4    5.7      2    7.6  229.6
 
  1980  6  19.3    10.2      0    77.2  195.8
 
  1980  7  19.3    10.8      0    55.9  161.8
 
  1980  8  21.3    12.1      0    56.4  155.3
 
  1980  9  19.5    11.1      0    13.4  155.6
 
  1980  10  13.3    5.4      3    58.3  136.5
 
  1980  11    8.9    3.7      4    41.2    55.7
 
  1980  12    8.2    2.2      8    29.6    64.7
 
  1981  1    7.4    1.7      12    35.8    50.4
 
  1981  2    6.1    -0.2      17    15.8    62.6
 
  1981  3  11.0    5.6      0    97.2    60.1
 
  1981  4  11.7    3.8      3    56.0  143.3
 
  1981  5  15.8    7.4      1    58.2  152.6
 
  1981  6  18.1    9.7      0    14.2  165.0
 
  1981  7  20.8    11.7      0    73.5  161.6
 
  1981  8  21.7    11.9      0    27.3  180.0
 
  1981  9  19.9    10.6      0    62.1  170.8
 
  1981  10  12.4    5.5      2    72.0  140.3
 
  1981  11  10.9    4.5      2    27.2    65.2
 
  1981  12    3.0    -2.7      19    31.2    39.6
 
  1982  1    5.6    -1.1      14    35.2    51.7
 
  1982  2    7.9    1.8      6    20.3    47.3
 
  1982  3  10.5    2.3      4    42.8  169.9
 
  1982  4  13.4    3.9      4    10.0  160.6
 
  1982  5  17.3    6.4      1    51.1  252.6
 
  1982  6  20.2    11.9      0  107.4  163.6
 
  1982  7  21.8    12.2      0    53.1  184.3
 
  1982  8  21.3    12.0      0    72.3  187.8
 
  1982  9  19.9    10.3      0    53.8  162.3
 
  1982  10  13.6    7.5      0  125.8    82.9
 
  1982  11  10.6    5.6      4    65.8    63.3
 
  1982  12    7.2    1.1      14    48.9    56.6
 
  1983  1    9.5    3.9      2    36.6    62.0
 
  1983  2    4.5    -1.0      17    42.7    95.2
 
  1983  3  10.2    3.2      1    35.4    87.8
 
  1983  4  11.6    3.0      3    86.2  147.9
 
  1983  5  14.8*    6.7      0  101.8  147.0
 
  1983  6  19.4*  10.2      0    33.3  193.1
 
  1983  7  25.8    13.5      0    48.6  242.4
 
  1983  8  23.4    12.6      0    10.5  220.8
 
  1983  9  18.2    10.9      0    70.3  127.1
 
  1983  10  14.7    7.1      3    33.5  134.7
 
  1983  11  10.4    4.8      6    45.4    38.8
 
  1983  12    8.3    2.8      8    35.8    60.9
 
  1984  1    6.9    0.6      9    61.9    96.5
 
  1984  2    6.2    0.6      8    42.0    67.0
 
  1984  3    8.0    1.9      4    31.0    44.9
 
  1984  4  13.4    2.0      5    13.6  231.0
 
  1984  5  14.0    5.1      0    76.1  130.3
 
  1984  6  19.5    10.0      0    48.9  210.0
 
  1984  7  22.3    11.2      0    15.0  213.6
 
  1984  8  23.6    12.4      0    60.9  199.1
 
  1984  9  17.5    10.5      0  113.3  103.5
 
  1984  10  15.6    7.6      0    48.9  113.5
 
  1984  11  11.2    5.9      0    87.6    49.0
 
  1984  12    8.2    2.6      9    35.1    67.5
 
  1985  1    3.4    -2.2      23    37.3    40.7
 
  1985  2    4.9    -1.9      13    14.6    79.0
 
  1985  3    8.7*    1.1      10    45.8    97.8
 
  1985  4  12.9*    4.9      2    31.2  139.3
 
  1985  5  15.4*    7.1      0    49.0  132.7
 
  1985  6  17.4*    8.7      0  107.0  142.9
 
  1985  7  21.8    12.4      0    38.5  194.4
 
  1985  8  20.1    11.2      0    50.9  158.4
 
  1985  9  20.1    10.6      0    18.6  123.1
 
  1985  10  15.6    7.3      1    12.4  111.9
 
  1985  11    7.1    1.1      11    35.3    93.3
 
  1985  12    9.2    4.8      4    73.1    29.0
 
  1986  1    6.3    0.8      9    50.2    75.0
 
  1986  2    0.8    -4.0      24    10.4    60.2
 
  1986  3    9.3    1.3      8    43.2  129.0*
 
  1986  4    9.8    2.7*      4*  63.6  116.3
 
  1986  5  16.3    7.4      0    74.7  189.8
 
  1986  6  20.7    9.6      0    13.2  200.8
 
  1986  7  21.7    12.0      0    43.3  172.3
 
  1986  8  19.1    10.4      0    70.9  137.8
 
  1986  9  17.1    6.5      0    29.3  166.9
 
  1986  10  15.6    6.4      1    69.7  114.6
 
  1986  11  11.0    4.4      1    54.5    75.9
 
  1986  12    9.0    2.7      3    54.2    62.8
 
  1987  1    2.8    -2.1      22    9.5    44.5
 
  1987  2    7.0*  -0.1      16    27.8    58.1
 
  1987  3    7.7*    0.0      17    44.9    97.4
 
  1987  4  15.2    5.6      0    42.9  151.5
 
  1987  5  14.9    5.3      0    46.9  166.8
 
  1987  6  17.5    9.2      0    93.5  106.6
 
  1987  7  20.6    11.9      0    70.2  148.1
 
  1987  8  20.5    11.7      0    71.9  130.8
 
  1987  9  18.8    10.1      0    30.5  151.5
 
  1987  10  14.1    6.1      2  116.4  120.7
 
  1987  11    9.0    4.0      2    39.9    42.0
 
  1987  12    8.2    3.3      6    23.5    29.3
 
  1988  1    8.0    2.7      6  100.3    58.1
 
  1988  2    7.9    2.0      5    20.9  123.0
 
  1988  3    9.7    3.1      4    59.3    86.6
 
  1988  4  12.5*    4.1      5    37.2  118.8
 
  1988  5  17.0    7.5      0    38.7  162.5
 
  1988  6  18.3    9.9      0    48.1  141.6
 
  1988  7  19.4    11.7      0    94.6  153.4
 
  1988  8  21.4    11.4      0    46.8  191.6
 
  1988  9  18.2*  10.0      0    45.8  137.9
 
  1988  10  14.6*    7.8      3    42.7  103.5
 
  1988  11    9.0*    1.2      13    31.0    79.5
 
  1988  12    9.6    4.9      0    23.6    45.6
 
  1989  1    8.9    3.1      4    25.8    73.7
 
  1989  2    9.6    2.6      5    47.5  103.5
 
  1989  3  12.3    4.1      2    40.0    98.1
 
  1989  4  10.6    3.1      2    75.8  123.7
 
  1989  5  19.7    7.2      0    6.1  269.1
 
  1989  6  21.2    9.5      0    40.5  215.8
 
  1989  7  24.8    13.2      0    43.0  225.5
 
  1989  8  24.1    12.1      0    28.8  238.2
 
  1989  9  20.5    11.8      0    17.7  116.6
 
  1989  10  16.5    9.1      0    32.2    94.9
 
  1989  11    9.9    2.5      6    35.3    98.3
 
  1989  12    8.3    3.1      5  125.4    22.9
 
  1990  1    9.8    4.0      0    43.8    64.7
 
  1990  2  11.4    4.7      1    71.1  102.0
 
  1990  3  12.9*    4.7      3    23.2  153.2
 
  1990  4  13.5    3.1      7    28.2  206.3
 
  1990  5  19.1    6.7      0    6.2  225.7
 
  1990  6  19.2    9.8      0    30.1  112.5
 
  1990  7  23.7    11.5      0    16.9  224.8
 
  1990  8  25.7    13.6      0    21.1  230.8
 
  1990  9  18.6    8.9      0    37.3  136.8
 
  1990  10  16.2    9.5      0    43.7  126.3
 
  1990  11    9.8    4.5      1    40.5    40.6
 
  1990  12    6.9    2.3      5    45.8    43.1
 
  1991  1    6.2    1.2      8    29.9    75.6
 
  1991  2    4.6    -1.8      20    23.0    45.3
 
  1991  3  12.0    4.7      2    37.9  100.3
 
  1991  4  12.4    4.0      3    42.9  147.1
 
  1991  5  14.7    6.6      1    13.9  114.5
 
  1991  6  16.7    8.8      0    97.6  113.5
 
  1991  7  22.9    13.1      0    32.1  192.7
 
  1991  8  23.7    12.8      0    41.2  209.8
 
  1991  9  20.5    9.8*      0*  46.5  176.1
 
  1991  10  14.1    6.9      1    18.5    88.9
 
  1991  11    9.8    4.2      3    54.9    51.0
 
  1991  12    7.3    1.2      12    11.9    43.8
 
  1992  1    6.4    1.4      10    67.8    49.5
 
  1992  2    9.2    2.0      8    14.0    56.1
 
  1992  3  11.1    4.6      0    58.0    74.0
 
  1992  4  13.2    4.9      1    40.7  115.7
 
  1992  5  19.8    8.1      0    34.3  234.2
 
  1992  6  21.7    10.7      0    35.8  185.2
 
  1992  7  21.8    13.1      0    54.4  138.8
 
  1992  8  21.2    12.2      0    72.4  163.0
 
  1992  9  18.4    10.0      0    82.0  127.7
 
  1992  10  11.5    5.1      0    91.6    97.9
 
  1992  11  10.8    4.2      1    78.2    62.5
 
  1992  12    6.4    0.5      15    31.6    42.5
 
  1993  1    9.6    2.9      5    51.3    39.8
 
  1993  2    6.6    2.3      3    11.6    37.4
 
  1993  3  10.7    3.1      7    19.9  125.0
 
  1993  4  13.4    6.3      0    78.4  108.4
 
  1993  5  17.2    7.6      0    47.9  185.7
 
  1993  6  20.4    10.5      0    86.0  186.6
 
  1993  7  20.5    11.5      0    54.4  169.1
 
  1993  8  20.5    10.6      0    37.5  196.3
 
  1993  9  16.7    9.6      0    67.4    95.1
 
  1993  10  12.1    6.2      5    88.9  107.6
 
  1993  11    7.9    2.1      11    53.5    59.3
 
  1993  12    8.5    2.6      5    78.3    43.5
 
  1994  1    8.5    2.3      7    70.5    66.6
 
  1994  2    6.7    0.0      13    34.4    82.8
 
  1994  3  11.7    4.2      0    36.1  134.1
 
  1994  4  12.4    4.3      3    67.3  164.5
 
  1994  5  15.1    7.1      0    44.1  135.9
 
  1994  6  20.3    10.3      0    20.0  219.6
 
  1994  7  25.8    13.1      0    23.0  222.1
 
  1994  8  22.1    13.0      0    36.4  176.9
 
  1994  9  16.6    10.1      0    72.1  103.2
 
  1994  10  13.9    7.9      0    74.9  108.2
 
  1994  11  12.6    7.0      1    27.3    36.6
 
  1994  12    9.8*    2.5      10    41.7    66.7
 
  1995  1    7.9*    1.7      6    86.5    56.9
 
  1995  2    9.6    3.3      2    63.1    84.4
 
  1995  3    9.9    1.6      12    50.8  185.8
 
  1995  4  13.9    4.5      4    10.2  161.7
 
  1995  5  17.9    6.2      1    24.4  202.5
 
  1995  6  18.7    9.3      0    17.3  159.7
 
  1995  7  25.8    13.4      0    27.1  234.9
 
  1995  8  25.7    13.1      0    5.3  243.7
 
  1995  9  18.0    10.4      0  105.4  114.8
 
  1995  10  17.6    9.5      1    12.7  149.4
 
  1995  11  10.9    5.0      3    29.4    67.3
 
  1995  12    4.3    -0.4*    15*  63.9    29.5
 
  1996  1    5.8    1.7      8    33.2    21.9
 
  1996  2    5.4    -1.0      18    45.8    82.6
 
  1996  3    7.2    1.0      9    19.5    59.7
 
  1996  4  13.7    3.8      6    4.5  148.9
 
  1996  5  13.9    4.3      6    19.8  155.8
 
  1996  6  21.5    9.4      0    12.4  225.6
 
  1996  7  23.2    11.7      0    40.5  207.4
 
  1996  8  22.6    12.6      0    63.9  176.3
 
  1996  9  18.4    10.1      0    5.6  130.8
 
  1996  10  15.9    8.2      0    31.2  143.0
 
  1996  11    9.3    2.7      12    70.9    92.1
 
  1996  12    5.1    0.3      15    30.2    52.5
 
  1997  1    4.7    -1.2      22    11.6    51.8
 
  1997  2  10.5    3.5      3    38.7    60.0
 
  1997  3  13.0    4.9      0    7.0  144.8
 
  1997  4  13.9    3.9      4    12.5  159.3
 
  1997  5  17.6    6.5      1    36.8  222.4
 
  1997  6  18.9    11.0      0  151.0  135.7
 
  1997  7  22.3    11.6      0    25.9  199.3
 
  1997  8  26.3    14.1      0    60.5  188.2
 
  1997  9  19.9    10.3      0    15.0  162.4
 
  1997  10  14.7    5.8      7    45.1  146.7
 
  1997  11  11.7    5.1      4    40.4    48.3
 
  1997  12    8.5    3.0      7    62.4    39.7
 
  1998  1    8.1    2.5      6    48.1    46.8
 
  1998  2  11.2    3.2      4    4.8  119.9
 
  1998  3  11.5    4.6      3    45.2    73.8
 
  1998  4  11.9    5.5      1  120.4  111.2
 
  1998  5  18.2    7.1      0    6.4  183.6
 
  1998  6  18.9    10.8      0  101.9  132.5
 
  1998  7  20.8    11.7      0    25.7  144.0
 
  1998  8  22.6    11.1      0    16.0  201.5
 
  1998  9  19.4    11.9      0  104.0  121.5
 
  1998  10  14.0    8.1      1    77.3  102.2
 
  1998  11    8.4    2.4      6    55.1    66.4
 
  1998  12    8.7    2.4      10    59.8    37.5
 
  1999  1    9.1    2.4      6    50.2    68.6
 
  1999  2    8.2    1.8      9    36.1    96.6
 
  1999  3  11.7    4.0      2    40.9  103.4
 
  1999  4  14.3    5.2      3    33.0  153.8
 
  1999  5  18.3    8.3      0    49.8  161.4
 
  1999  6  19.2    9.7      0    89.0  197.9
 
  1999  7  24.5    12.5      0    24.5  233.2
 
  1999  8  21.6    12.4      0    92.5  138.7
 
  1999  9  21.3    11.9      0    76.4  148.5
 
  1999  10  14.9    7.3      0    43.7  137.0
 
  1999  11  10.6    5.1      0    25.3    63.3
 
  1999  12    7.8    1.8      8    63.1    76.1
 
  2000  1    8.1    1.8      11    20.5    78.6
 
  2000  2    9.8    2.8      5    50.7  106.4
 
  2000  3  11.2    4.0      2    14.6  109.2
 
  2000  4  12.4    4.5      4    85.9  138.0
 
  2000  5  16.8    8.5      0    83.8  161.2
 
  2000  6  20.5    11.3      0    17.5  162.0
 
  2000  7  20.1    11.8      0    60.7  126.9
 
  2000  8  23.1    12.6      0    21.1  207.7
 
  2000  9  20.0    12.6      0    68.1  126.5
 
  2000  10  14.0    8.0      0  108.0    98.7
 
  2000  11  10.4    3.8      2  108.8    69.6
 
  2000  12    8.1    3.5      9    59.6    50.0
 
  2001  1    6.1    0.4      14    47.2    83.8
 
  2001  2    8.2    0.9      14    72.4    77.2
 
  2001  3    9.0    2.0      8    77.4    87.7
 
  2001  4  12.1    3.9      1    62.6  132.0
 
  2001  5  18.5    7.2      0    17.5  220.7
 
  2001  6  19.9    9.6      0    22.8  194.0
 
  2001  7  23.1    13.2      0    55.1  178.7
 
  2001  8  22.9    13.3      0    65.8  180.5
 
  2001  9  17.3    10.1      0    68.9  114.9
 
  2001  10  17.3    10.9      0  128.9  107.7
 
  2001  11  10.6    4.0      4    36.4    73.7
 
  2001  12    6.2    0.4      18    19.3    72.0
 
  2002  1    8.9    2.6      8    33.0    48.8
 
  2002  2  10.7    3.7      4    50.4    82.5
 
  2002  3  12.0    3.0      7    30.6  122.9
 
  2002  4  14.7    4.2      2    33.4  196.2
 
  2002  5  16.6    7.9      0    53.5  180.2
 
  2002  6  20.1    10.3      0    28.5  182.1
 
  2002  7  22.0    12.3      0    94.6  172.2
 
  2002  8  22.9    13.2      0    42.0  143.8
 
  2002  9  19.6    10.0      0    23.5  147.6
 
  2002  10  14.3    6.7      0    67.7    94.4
 
  2002  11  11.4    5.7      2    85.9    47.3
 
  2002  12    8.2    3.9*      4*  85.1    20.4
 
  2003  1    7.0    1.6      12    78.6    53.0
 
  2003  2    7.8    0.8      12    12.9  101.5
 
  2003  3  12.9    2.4      8    13.7  164.0
 
  2003  4  15.1    4.5      4    24.2  178.3
 
  2003  5  17.5    7.5      0    39.9  190.0
 
  2003  6  22.1    11.5      0    60.7  210.0
 
  2003  7  23.7    13.3      0    66.8  184.6
 
  2003  8  25.3    13.3      0    2.0  214.1
 
  2003  9  21.3    8.9      0    15.7  196.3
 
  2003  10  14.0    4.8      1    30.0  141.8
 
  2003  11  11.7    5.9      1    80.8    72.9
 
  2003  12    8.1    2.3      7    46.0    57.8
 
  2004  1    8.1    2.7      7    69.6    49.2
 
  2004  2    8.1    3.3      11    34.3    72.6
 
  2004  3  10.5    3.2      3    24.7    96.2
 
  2004  4  14.2    5.5      0    41.4  138.9
 
  2004  5  16.9    7.9      0    44.5  162.7
 
  2004  6  21.2    11.2      0    34.0  190.9
 
  2004  7  22.0    11.6      0    59.3  173.8
 
  2004  8  24.0    14.1      0    70.7  184.8
 
  2004  9  20.6    11.1      0    18.7  169.8
 
  2004  10  15.0    7.8      0    67.2    99.0
 
  2004  11  10.5    5.5      3    45.4    56.6
 
  2004  12    8.4    2.5      9    32.2    51.4
 
  2005  1    9.2    3.1      5    28.3    66.3
 
  2005  2    7.1    1.9      9    22.3    67.0
 
  2005  3  10.7    3.8      7    19.8    66.0
 
  2005  4  14.2    4.3      4    27.7  130.6
 
  2005  5  16.6    7.0      0    47.4  197.6
 
  2005  6  21.8    10.9      0    47.1  175.8
 
  2005  7  22.0    12.8      0    43.7  157.5
 
  2005  8  22.6    11.5      0    53.3  182.5
 
  2005  9  21.1    11.3      0  166.2  148.1
 
  2005  10  17.4    10.5      0    61.2  107.1
 
  2005  11  10.1    3.0      9    47.2  101.0
 
  2005  12    7.8    1.8      10    18.0    75.1
 
  2006  1    7.0    1.5      12    19.8    48.8
 
  2006  2    7.0    1.5      5    25.2    71.1
 
  2006  3    9.0    1.7      12    35.9  105.2
 
  2006  4  13.5    5.1      3    30.1  144.5
 
  2006  5  17.4    8.7      0    62.8  155.7
 
  2006  6  22.5    11.2      0    18.9  220.2
 
  2006  7  28.3    14.1      0    45.1  253.5
 
  2006  8  21.5    12.2      0    74.9  152.5
 
  2006  9  22.7    13.4      0    50.0  164.7
 
  2006  10  17.5    10.5      0    43.2  111.4
 
  2006  11  12.5    5.1      2    64.8  109.0
 
  2006  12    9.7    3.3      6    50.5    41.0
 
  2007  1  10.4    3.8      5    68.5    66.2
 
  2007  2    9.7    2.3      8    50.9    70.4
 
  2007  3  12.1    3.2*      1*  25.7  161.1
 
  2007  4  17.1    6.0*      1*    1.0  183.1
 
  2007  5  16.9    8.3*      0*  124.3  121.7
 
  2007  6  20.4    11.1      0    59.0  148.9
 
  2007  7  21.6    11.0      0    62.1  189.7
 
  2007  8  21.6    10.8      0    51.1  180.4
 
  2007  9  19.5    10.0      0    25.2  158.1
 
  2007  10  15.2    7.2      0    56.4    97.3
 
  2007  11  11.1    3.7      5    36.3    77.2
 
  2007  12    8.5    1.9      12    38.2    51.9
 
  2008  1  10.3    3.8      0    58.0    63.2
 
  2008  2    9.9    0.6      11    12.8  105.2
 
  2008  3  10.1    3.0      5    68.9  117.0
 
  2008  4  13.1    3.8      3    50.8  158.2
 
  2008  5  19.0    7.9      0    62.9  173.9
 
  2008  6  19.7    10.4      0    34.6  181.8
 
  2008  7  22.8    12.4      0    52.1  188.1
 
  2008  8  22.3    13.3      0    64.7  122.7
 
  2008  9  18.6    9.6      0    59.5  103.3
 
  2008  10  14.4    6.1      1    56.2  130.3
 
  2008  11  10.3    4.7      5    68.8    55.5
 
  2008  12    6.6    1.4      11    21.3    68.0
 
  2009  1    5.8    0.2      13    40.9    46.2
 
  2009  2    7.1    1.8      13    47.9    42.1
 
  2009  3  11.9    2.8      6    32.2  149.7
 
  2009  4  15.6    5.4      0    12.6  166.1
 
  2009  5  18.3    7.7      0    28.4  204.0
 
  2009  6  20.9    10.2      0    40.8*  198.8*
 
  2009  7  22.5    12.1      0    71.0  196.9*
 
  2009  8  23.6    13.1      0    58.6  200.0*
 
  2009  9  20.1    10.5      0    10.0  152.5*
 
  2009  10  15.7    8.3      0    32.2    87.8*
 
  2009  11  12.0    6.5      0    89.8    78.7*
 
  2009  12    6.2    0.6      12    63.2    68.5*
 
  2010  1    3.9    -0.4      15    46.6    55.2*
 
  2010  2    6.1    0.4      12    70.6    54.9*
 
  2010  3  10.9    2.8      9    26.2  118.4*
 
  2010  4  14.9    3.7      3    12.5  212.1*
 
  2010  5  16.5    5.9      2    28.6  209.4*
 
  2010  6  21.6    10.0      0    25.4  229.9*
 
  2010  7  24.6    13.8      0    10.8    ---
 
  2010  8  21.0    12.2      0  133.2    ---
 
  2010  9  19.0    10.5      0    46.4    ---
 
  2010  10  14.7    7.7      0    39.0    ---
 
  2010  11    8.4    3.0      9    22.4    ---
 
  2010  12    2.8    -2.7      23    19.8    ---
 
  2011  1    6.8    2.2      7    52.4    ---
 
  2011  2    9.6    4.0      2    28.2    ---
 
  2011  3  11.6    2.7      5    1.8    ---
 
  2011  4  18.6    6.5      0    2.0    ---
 
  2011  5  18.7    7.8      1    12.8    ---
 
  2011  6  20.3    9.9      0    53.0    ---
 
  2011  7  21.2    10.9      0    38.4*    ---
 
  2011  8  21.6    11.9      0    40.2    ---
 
  2011  9  21.2    11.3      0    31.6    ---
 
  2011  10  17.9    9.3      0    15.8    ---
 
  2011  11  13.3    6.6      2    28.0    ---
 
  2011  12    9.1    3.2      3    43.0    ---
 
  2012  1    9.1    2.8      7    39.0    ---
 
  2012  2    7.0    0.1      12    17.4    ---
 
  2012  3  13.3    2.6      5    23.4    ---
 
  2012  4  12.2    3.9      2    95.6    ---
 
  2012  5  16.9    7.9      0    42.6    ---
 
  2012  6  18.5    10.4      0    91.4    ---
 
  2012  7  21.2    12.0      0  101.4    ---
 
  2012  8  23.5    12.9      0    39.2    ---
 
  2012  9  19.3    8.9      0    30.0    ---
 
  2012  10  13.5    6.9      0    78.6    ---
 
  2012  11  10.0    4.0      2    71.0    ---
 
  2012  12    8.1    2.1      10    97.7*    ---
 
  2013  1    5.8    0.5      16    39.8    ---
 
  2013  2    6.0    0.6      9    19.6    ---
 
  2013  3    6.2    0.1      16    38.6    ---
 
  2013  4  13.2    3.4      11    26.4    ---
 
  2013  5  15.9    6.1      1    52.0    ---
 
  2013  6  19.2    9.7      0    14.2    ---
 
  2013  7  25.1    12.8      0    32.8    ---
 
  2013  8  23.5    13.0      0    48.2    ---
 
  2013  9  19.0    10.0      0    34.8    ---
 
  2013  10  16.2    9.8      0    78.2    --- 
 
  2013  11    9.6    3.7      2    40.2    --- 
 
  2013  12    9.7    3.1      1    36.0    --- 
 
  2014  1    9.2    3.1      4  103.6    --- 
 
  2014  2    9.8    3.8      0    61.2    --- 
 
  2014  3  13.4    3.4      4    17.4    --- 
 
  2014  4  15.9    6.3      2    13.8    --- 
 
  2014  5  17.7    8.7      0    84.6    --- 
 
  2014  6  20.8    10.7      0    44.4    --- 
 
  2014  7  24.4    13.5      0    49.2    --- 
 
  2014  8  21.0    11.8      0  126.8    --- 
 
  2014  9  20.5    11.0      0    23.0    --- 
 
  2014  10  17.0    9.9      0    67.2    --- 
 
  2014  11  11.9    5.8      3    58.4    --- 
 
  2014  12    8.5    2.8      8    40.2    ---    Provisional
 
  2015  1    8.0    1.3      12    48.4*  ---    Provisional
 
  2015  2    7.4    1.2      10    30.8    ---    Provisional
 
  2015  3  11.1    3.1      4    19.4    ---    Provisional
 
 
 
</source>
 
 
<br />
 
<br />
 
==rainyCambridge.py==
 
==rainyCambridge.py==

Revision as of 18:25, 29 November 2015

--D. Thiebaut (talk) 10:34, 18 September 2015 (EDT)


Wed., 9/16/15


# displayWeek.py
# D. Thiebaut
# displays a schedule for the five days of the week

length = eval( input( "Length of bar? " ) )

print( "-" * length )
for day in [ "Mo:", "Tu:", "We:", "Th:", "Fr:" ]:
    print( day )
    #print( "  :" * 2)
    print( "  :" )
    print( "-" * length )


Friday, 9/18/15


# displayGrade.py
# D. Thiebaut
#
# prompts user for information and
# grade.
# display user's grade in graph, along with
# class average (constant)


# constant information
classAvg = 80

# user input.  Get info from user
fName = "Al"
lName = "K"
Id    = "990123456"
final = 90


#--- Format information and display ---
# create the bar 
bar = "+" + 48*"-" + "+"
lenBar = len( bar )

# compute the number of dashes
noSpaces = lenBar - len( fName ) -len(lName )-len(Id) - 6
spaces = " " * noSpaces

# create the scale
scale = "      00...10...20...30...40...50...60...70...80...90...100"

# display the information
print( bar )
print( "|" + fName, lName, spaces, Id, "|" )
print( bar )
print()
print( scale )
# the length of the bar for the bar-graph, in number of characters
# is 1/2 the actual grade.  So divide the grade by half to get the
# number of chars.  We use // to get the integer part of the result
print( "grade:", (final//2) * "#" )
print( "class:", (classAvg//2) * "#" )


Monday, 9/21/15


Version 1 of Teller Machine program

# tellerMachine.py
# D. Thiebaut
# A program that simulates a teller machine, where user enters an amount of
# dollars, and program figures out the number of bills to return.

# get the amount 
amount = eval( input( "Please enter amount: " ) )
amount = int( amount )
print()

# break down into bills
no20s = amount // 20       # how many 20s go into amount
amount = amount % 20    # what is left over after giving out 20s go back into amount

no10s = amount // 10
amount = amount % 10

no5s = amount //5
no1s = amount % 5

# display the results
print( "Amount withdrawn = ", amount )
print( "Please lift your keyboard and find: " )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )


Version 2, submitted by Shirui Cheng, who figured out that it was a perfect opportunity to use a loop!

# Teller machine simulator
# Shirui Cheng

# get the input
amount = eval(input ("How much money do you want to withdraw? "))

# break down in $20-, $10-, $5-, and $1-bills
# and print the result at every step
for bill in (20, 10, 5, 1):
    no = amount // bill
    amount = amount % bill
    print (no, "$", bill, "bill(s)")


Wednesday, 9/23/15


# averageAge.py
# D. Thiebaut
#
# Example of how one would go about computing
# the average value of a list of numbers.
#
def main():

    # initialize variables
    sum = 0
    count = 0

    # loop through a list of ages and compute the total sum
    # of the ages, and the number of values in the list.
    for age in [20, 19, 21, 20, 21, 29, 17]:
        sum = sum + age
        count = count + 1

    # compute the average, displays quantities of interest
    print( "-" * 30 )
    print( "average = ", sum/count )

main()


Accumulating Strings


# stringPatterns.py
# D. Thiebaut
# print a string of 5 alternating patterns.

def main():
    # define 2 different patterns
    pat1 = "**"
    pat2 = "++"

    # create an empty string
    result = ""

    # loop 5 times (we want a string of 5 alternating patterns)
    for i in range( 5 ):
        # add a new pattern to the string
        result = result + pat1

        # switch the patterns around
        pat1, pat2 = pat2, pat1

    # done with the loop!  Print string of patterns
    print( result )

main()


Friday, 9/25/15


# averageAge.py
# D. Thiebaut
#
# Example of how one would go about debugging a simple
# program and reveal how the loop works.
#
def main():

    # initialize variables
    sum = 0
    count = 0
    print( "sum = {0:1} count = {1:1}".format( sum, count ) )
    input()

    # loop through a list of ages and compute the total sum
    # of the ages, and the number of values in the list.
    for age in [20, 19, 21, 20, 21, 29, 17]:
        sum = sum + age
        count = count + 1
        print( "age = {0:5} sum = {1:5} count = {2:5}".format( age, sum, count ) )
        input()

    # compute the average, displays quantities of interest
    print( "-" * 30 )
    print( "sum = ", sum )
    print( "count = ", count )
    print( "average = ", sum/count )

main()



Wed., 9/30/15


# exercises 9/30/15

def main():
    # Exercise #1
    fName = "SOPHIA"
    lName = "smith"

    fullName = fName + " " + lName

    print( fullName.title().center( 60 ) )
    print( "\n", "-"*60, "\n\n", sep="" )
   
    # Exercise #2
    # Define the string with multiple lines
    book = """Ulysses
    James Joyce
    Stately, plump Buck Mulligan came from the stairhead, 
    bearing a bowl of lather on which
    a mirror and a razor lay crossed. 
    """

    # split the book into a list of lines
    lines = book.splitlines()

    # assign different lines to variables
    bookTitle = lines[0]
    author = lines[1]
    sentence = lines[2] + lines[3] + lines[4]
    # this next statement is too sophisticated for now
    #sentence = " ".join( lines[2: ] )

    # display the result
    print( bookTitle.upper().center( 60 ) )
    print( author.title().center( 60 ) )
    print( )
    print( sentence )

main()


Fri., 10/2/15


# Using split()
# -------------------------
poem = """Chocolate
Chocolate is the first luxury.
It has so many things wrapped up in it:
Deliciousness in the moment,
childhood memories,
and that grin-inducing
feeling of getting a reward for being good.
--Mariska Hargitay"""

# display each line centered in 60 spaces.
# first line all uppercase.
# last line right justified in 60 spaces.

lines = poem.split( "\n" )
#print( lines )

# -----------------------------------------------------------------------
# (remove the triple double-quotes to energize the code section)
print( lines[0].upper().center(60) )

for line in lines[1: ]:
    print( line.center( 60 ) )

# -----------------------------------------------------------------------
print( "-" * 60 )

lines[0] = lines[0].upper()
for line in lines[0:3]+lines[4:]:
    print( line.center(60) )


Another Example


# -------------------------
# Another Example
# -------------------------


def printBar():
    print( 60 * '-' )


def sayHello():
    print( )
    print( "Hello, and welcome!" )
    print( )


def main():
    for i in range( 10 ):
        printBar() 
    sayHello()
    printBar()

main()


Happy Birthday


# ------------------------------------------
# Sing Happy Birthday to some Minions
# ------------------------------------------

def singHappyBirthday( minion ):
    print( "Happy birthday to you\n" * 2, end="" )
    print( "Happy birthday, dear", minion )
    print( "Happy birthday to you" )
    print()
    
def main():
    #singHappyBirthday( "Dave" )
    #singHappyBirthday( "Stuart" )
    minions = "Dave, Stuart, Jerry, Jorge, Tim, Mark, Phil, Kevin,  Jon"

    minions = minions.split( ", " )
    #print( minions )
    
    for minion in minions:
        singHappyBirthday( minion.strip() )
        
main()


Mon. 10/5/15


Converting Temperatures


# F  = C * 9 / 5 + 32
def convert( c ):
    return int( c * 9/5 + 32 )


def main():
    for C in [ 0, 32, 100 ] :
        fahr = convert(  C )
        print( fahr )


main()


Transforming dates


def singHappyBirthday( minion ):
    print( "minion = ", minion )
    #print( "Happy birthday to you\n" * 2, end="" )
    #print( "Happy birthday, dear", minion )
    #print( "Happy birthday to you" )
    #print()

def worker0( ):
    return "clap " *  10  

def worker1( num1 ):
    return num1 * 2 + 1 

def worker2( n ):
    return n % 10 

def worker3( p ):
    p = worker1( p )
    p = worker2( p )
    return p 
    
def dateConvert( date ):
    """converts a date in the form "2 Jan 2010" into
    a date of the form 01022010"
    """
    # split the string into day, month, and year
    fields = date.split()
    day = fields[0]
    month = fields[1]
    year = fields[2]

    #print( day, month, year )

    # if the day is less than 10, add a 0 in front of it
    day = ( '0' + day )[-2: ]
    
    #print( day, month, year )

    # find where the month is located in a string of 3-letter
    # months.  If the string is "Jan", then the location will
    # be 0.  If the string is "Feb", then the location will be 3,
    # etc.
    months = "janfebmaraprmayjunjulaugsepoctnovdec"
    index  = months.find( month.lower() )

    # divide by 3 to find the index in the form 0, 1, 2,... 11.
    # and add 1 to make this 1, 2,... 12
    monthDigit = index//3 + 1

    # transform this digit into a string, with a 0 in front
    # in case the digit is less than 10.
    monthDigit = "{0:02}".format( monthDigit )
    
    #print( day, month, year, monthDigit )

    # return the formatted string
    return "{0:1}{1:1}{2:1}".format( monthDigit, day, year )


def main():

    for date in [ "1 Jan 2010", "2 Mar 1900", "31 Dec 2015" ]:
        print( dateConvert( date ) )

    
main()
  • Output:


01012010
03021900
12312015


Fri. 10/9/15


Once More: the Teller Machine program

def getAmount():
    amount = eval( input( "Amount? " ) )
    return abs( amount )

def breakDown( amount ):
    no20s, amount = amount // 20, amount % 20
    no10s, amount = amount // 10, amount % 10
    no5s, amount   = amount // 5, amount % 5
    no1s = amount

    return no20s, no10s, no5s, no1s

def display(orgAmount, No20s, No10s, No5s, No1s ):
    print( orgAmount, No20s, No10s, No5s, No1s )
    return 
    
def main():
    # get amount from user
    amount = getAmount( )
    orgAmount = amount    # keep value of original amount
    
    # get Number of $20-, $10, $5-, and $1-bills
    No20s, No10s, No5s, No1s = breakDown( amount )

    # display result
    display( orgAmount, No20s, No10s, No5s, No1s )

main()


Flipping DNA


def getSentence(  ):
    sentence = input( "Enter a list of words: " )

    words = sentence.split( )
    for i in range( len( words ) ):
        #print( "i = ", i, "words = ", words )
        #print( "words[i] = ", words[i] )
        #print( "words[i].capitalize() = ", words[i].capitalize() )
        words[i] = words[i].capitalize()
        #print( "i = ", i, "words = ", words )
        #print()
        
    return words

def flip( DNA ):
    newDNA = DNA.replace( "A", "t" ).replace( "T", "a" )
    newDNA = newDNA.replace( "G", 'c' ).replace( "C", 'g' )
    return newDNA.upper()
        
def main():

    listWords = getSentence( )
    print( "list = ", listWords )

    print( "AAACGTTTAG", flip( "AAACGTTTAG" ), sep="\n" )
    
main()


Wed 10/14/15


dempolls.raw


<html>
<head>
<title>
Democratic Polls
</title>
</head>
<body>
<h1>Results</h1>
<data />
</body>
</html>


dempolls.data


Biden 19.1
Chafee 0.6
Clinton 44.4
Lessig 0.0
O'Malley 1.0
Sanders 25.1
Webb 1.2


createDynamicWebPage.py


# createDynamicWebPage.py
# D. Thiebaut
# create dynamic Web page


import datetime  # get a library that will allow
                 # us to print today's date.

# getTextFrom(): given a file name, opens the
# file, gets its contents as a string, and return
# it.
def getTextFrom( fileName ):
    file = open( fileName, "r" )
    text = file.read()
    file.close()
    return text

def main():

    # get the raw html file
    html = getTextFrom( "dempolls.raw" )

    # get the raw data file
    candidates = getTextFrom( "dempolls.data" )

    # replace <data /> tag with the data just
    # read in the raw html file.
    today = datetime.datetime.today().strftime("%m/%d/%Y")
    newString = today + "<br />" + candidates
    html = html.replace( "<data />", newString )

    # store new html to file
    file = open( "dempolls.html", "w" )
    file.write( html )
    file.close()

    # tell the user that process is over
    print( "File dempolls.html created, and in your directory!" )
    
main()


Wed. 10/21/15


graphicsDemo.py


# graphicsDemo.py
# D. thiebaut
# A demo program using the graphics library to make a ball
# move on the graphic window.

from graphics import *
from random import *

def main():
    # open the graphic window
    win = GraphWin("My Circle", 600, 600)

    # put some text in the middle of the window
    label = Text( Point( 300, 300 ),
                  "CSC111 Graphics Demo" )
    label.draw( win )
    
    # draw a circle
    x = 50
    y = 50
    r = 20
    c = Circle( Point( x, y ), r )
    red = randint( 0, 255 )
    green = randint( 0, 255 )
    blue = randint( 0, 255 )
    color = color_rgb( red, green, blue )
    c.setFill( color )
    c.draw( win )
    dx = 3
    dy = 0

    for i in range( 1000 ):
        # pick random color
        red = randint( 0, 255 )
        green = randint( 0, 255 )
        blue = randint( 0, 255 )
        color = color_rgb( red, green, blue )
        c.setFill( color )

        # move
        c.move( dx, dy )

        # see if bounce off walls
        center = c.getCenter()
        x = center.getX()
        if x > 600-r or x < 0+r:
            dx = -dx
        #win.getMouse()

        # move label
        label.move( 1, -1 )
        
    # wait for user to click in window
    win.getMouse() 

    # close the window
    win.close()

main()


Mon. 11/2/15


Exception, Version 1


# getInput: returns an integer larger
# than 0.  Expected to be robust…

def getInput():
  while True:
      x = int( input( "Enter a positive int:  " ) )        
      if  x >= 0:
          return x
      print( "invalid input.  Please reenter" )


def main():
   num = getInput()
   print( "you entered", num )

main()


Exception, Version 2


# getInput: returns an integer larger
# than 0.  Expected to be robust…

def getInput():
  while True:
      try:
          x = int( input( "Enter a positive int:  " ) )
      except ValueError:
          print( "Invalid number.  Please reenter" )
          continue
        
      if  x >= 0:
          return x
      print( "invalid input.  Please reenter" )


def main():
   num = getInput()
   print( "you entered", num )

main()


Quadratic Equation with Exceptions


import math

def ZelleExample():
    print( "solution to quadratic equation" )
    try:
        a, b, c = eval( input( "Enter 3 coefficients (a, b, c): " ) )
        disc = math.sqrt( b*b - 4*a*c )
        root1 = (-b + disc )/ (2*a)
        root2 = (-b - disc )/ (2*a)
        print( "solutions: ", root1, roo2 )
    except NameError:
        print( "You didn't enter 3 numbers!" )
    except TypeError:
        print( "Your input contained invalid numbers" )
    except SyntaxError:
        print( "Forgot a comma between the numbers? " )
    except ValueError:
        print( "No real roots, negative discriminant" )
    except:
        print( "Something went wrong..." )

def main():
    ZelleExample()

main()


Dice: Definining Classes


# DiceExample.py
# D. Thiebaut
# our first program where we define a class.
#
# The class implement a die, with some number
# of sides, and a way to "roll" and show a new
# number (value) up top.

# libraries
import random

# a class for a die
class Die:
    def __init__( self, n ):
         self.noSides = n
         self.value      = 1

    def roll( self ):
         self.value = random.randrange( 1, self.noSides+1 )

    def getValue( self ):
         return self.value


def main():
    # Create 2 dice, one with 6 sides, one with 8
    d1 = Die( 6 )
    d2 = Die( 8 )

    # Roll both dice
    d1.roll( )
    d2.roll( )

    # display their value
    print( "Die 1: ",  d1.getValue() )
    print( "Die 2: ",  d2.getValue() )

main()


A program with a class to implement Cat objects


# cats1.py
# D. Thiebaut
# Our first built-from scratch program involving
# classes and objects.
# In this program we read a CSV file containing
# the definitions of cats, one cat per line.
# We extract the name, vaccinated status, breed,
# and age of the cat, and save them in a cat
# object.  the Cat class is used to define how
# a cat object behaves.

class Cat:
    def __init__( self, na, vac, bre, ag ):
        self.name= na
        self.vaccinated = vac
        self.breed = bre
        self.age = ag

    def isVaccinated( self ):
        #if self.vaccinated == True:
        #    return True
        #else:
        #    return False
        return self.vaccinated

    def getName( self ):
        return self.name
    
def main():
    # Minou, 3, vac, stray
    text = """Minou, 3, vac, stray
    Max, 1, not-vac, Burmese
    Gizmo, 2, vac, Bengal
    Garfield, 4, not-vac, Orange Tabby"""

    catList = [] 
    for line in text.split( "\n" ):
        words = line.split( "," )
        if words[2].find( "not" ) != -1:
            vac = False
        else:
            vac = True
        age = int( words[1].strip() )
            
        cat = Cat( words[0], vac, words[3], age )
        catList.append( cat )
        
    #cat1 = Cat( "Minou", True, "stray", 3 )
    for cat in catList:    
        if cat.isVaccinated():
            print( cat.name, "is vaccinated" )
        else:
            print( cat.getName(), "is not vaccinated" )

main()


Cats1.csv File


Max, 1, vaccinated, not tattooed, stray
Black, 3, not vaccinated, tattooed, stray
Gizmo, 2, vaccinated, not tattooed, Bengal
Winston, 2, vaccinated, tattooed, Bengal
Garfield, 3, vaccinated, not tattooed, Burmese
Bob, 4, not vaccinated, tattooed, Orange Tabby
Minou, 1, vaccinated, tattooed, stray
Silky, 3, not vaccinated, tattooed, Siamese


Wed. 11/04/15


cats1.py


# cats1.py
# D. Thiebaut
# Our first built-from scratch program involving
# classes and objects.
# In this program we read a CSV file containing
# the definitions of cats, one cat per line.
# We extract the name, vaccinated status, breed,
# and age of the cat, and save them in a cat
# object.  the Cat class is used to define how
# a cat object behaves.

class Cat:
    def __init__( self, na, vac, bre, ag ):
        self.name= na
        self.vaccinated = vac
        self.breed = bre
        self.age = ag

    def isVaccinated( self ):
        #if self.vaccinated == True:
        #    return True
        #else:
        #    return False
        return self.vaccinated

    def getBreed( self ):
        return self.breed
    
    def getName( self ):
        return self.name

    def __str__( self ):
        if self.vaccinated == True:
            vacc = "vaccinated"
        else:
            vacc = "not vaccinated"
        return "{0:1}: {1:1} {2:1} {3:1} yrs old".format(
            self.name, vacc, self.breed, self.age )
    

def getCatList( fileName ):
    """parses a collection of lines and creates
    a list of cat objects."""

    file = open( fileName, "r" )
    text = file.read()
    file.close()

    catList = [] 
    for line in text.split( "\n" ):
        words = line.split( "," )
        
        if len( words ) != 4:
            continue
        
        if words[2].find( "not" ) != -1:
            vac = False
        else:
            vac = True
        age = int( words[1].strip() )
            
        cat = Cat( words[0].strip(), vac, words[3].strip(), age )
        catList.append( cat )
        
    return catList

def main():
    fileName = input( "File name? " )
    catList = getCatList( fileName )
    for cat in catList: print( cat )

    for cat in catList:
        if not cat.isVaccinated() and cat.getBreed()=="stray":
            print( cat )
        
    return
main()


cats2.csv


Minou, 3, vac, stray
Max, 1, not-vac, Burmese
Gizmo, 2, vac, Bengal
Garfield, 4, not-vac, Orange Tabby
Smudges, 10, not-vac, stray
Minique, 5, vac, French Tabby



Fri. 11/06/15


Wheel1.py


The beginning of a car...

# Graphics with Objects.
# Demo program illustrating how we are going to
# build a car.
# 
from graphics import *

#define global geometry of window
WIDTH = 800
HEIGHT = 600


def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Wheel Demo", WIDTH, HEIGHT )

    c1 = Circle( Point( WIDTH//2, HEIGHT//2 ), 30 )
    c1.draw( win )


    win.getMouse()
    win.close()

main()


Car1.py


# Graphics with Objects.
# Demo program illustrating how we are going to
# build a car.
# 
from graphics import *

#define global geometry of window
WIDTH = 800
HEIGHT = 600

class Wheel:
    def __init__( self, center, radius ):
        self.c1 = Circle( center, radius )
        self.c2 = Circle( center, radius//2 )
        self.c1.setFill( "black" )
        self.c2.setFill( "white" )

    def draw( self, win ):
        self.c1.draw( win )
        self.c2.draw( win )

    def move( self, dx, dy ):
        self.c1.move( dx, dy )
        self.c2.move( dx, dy )

    

class Car:
    def __init__( self, refPoint ):
        x1 = refPoint.getX()
        y1 = refPoint.getY()
        x2 = x1 + 180
        y2 = y1 + 60
        self.body = Rectangle( Point( x1 , y1 ), Point( x2 , y2 ) )
        self.w1 = Wheel( Point( x1+40, y1+60 ), 20 )
        self.w2 = Wheel( Point( x1+140, y1+60 ), 20 )
        self.body.setFill( "yellow" )

    def draw( self, win ):
        self.body.draw( win )
        self.w1.draw( win )
        self.w2.draw( win )

    def move( self, dx, dy ):
        self.body.move( dx, dy )
        self.w1.move( dx, dy )
        self.w2.move( dx, dy )
                  
        
def main():
    """Open a graphic window and puts graphic object(s)
    on it.  Wait for user to click mouse to close.
    """
    global WIDTH, HEIGHT

    # open window
    win = GraphWin( "Wheel Demo", WIDTH, HEIGHT )

    # Create a list of 10 cars
    cars = [] # empty list
    for i in range( 10 ):
        # create a new car
        c1 = Car( Point( i*10, i*90 ) )

        # draw the car just created
        c1.draw( win )
        
        # add car just created to the list
        cars.append( c1 ) 

    dx = 3 # x-direction for movement of cars
    dy = 0 # y-direction for movement of cars

    # animation loop
    while True:
        # move all the cars in the list
        for c1 in cars:
            c1.move( dx, dy )

        # if user clicks the mouse, change the
        # direction of movement.
        if win.checkMouse() != None:
            dx = -dx
        
    # wait for user to click the mouse to close window
    win.getMouse()
    win.close()

main()



Mon. 11/9/15


Images to Play With


catGlasses.gif


CatGlasses.gif


imageProcessing.py


# imageProcessingSkel.py
# D. Thiebaut
# A skeleton program to start doing image processing
# in Python
from graphics import *
# image geometry = 424x18
# make the window the same geometry

WIDTH  = 424
HEIGHT = 418

def main():
    # open the window
    win = GraphWin( "CSC111 Image Viewer", WIDTH, HEIGHT )

    # get the file name from the user
    fileName = "catGlasses.gif" #input( "Image name? " )
    print()
    
    # open the cat image
    cat = Image( Point(WIDTH//2, HEIGHT//2), fileName )
    cat.draw( win )

    # wait for user to click the mouse...
    win.getMouse()

    # and close the window
    win.close()

main()


pixelProcessing.py


# pixelProcessingSkel.py
# D. Thiebaut
# Simple skeleton program for processing
# the pixels of an image

from graphics import *

WIDTH  = 424   
HEIGHT = 418   
IMAGEFILENAME = "catGlasses.gif" 


    
def makeRed( img, win ):
    """ set the red component of all the pixels to 255, the
    maximum value.
    """
    global WIDTH, HEIGHT

    for y in range( HEIGHT ):
        win.update()  # refresh the window after each inner loop
        for x in range( WIDTH ):
            red, green, blue = img.getPixel( x, y )
            green, blue, red = red, green, blue
            red = 255
            img.setPixel( x, y, color_rgb(red, green, blue ) )


def main():

    win = GraphWin( "CSC111 Image Viewer", WIDTH, HEIGHT )

    img = Image( Point(WIDTH//2, HEIGHT//2), IMAGEFILENAME )
    img.draw( win )

    makeRed( img, win )
    
    win.getMouse()
    win.close()

main()


Wed. 11/11/15


chessboard0.py


# chessboard0,py
# D. Thiebaut
# Displays an 8x8 chessboard.

from graphics import *

WIDTH = 600
HEIGHT = 600

            
def displayBoard( win ):
    """display an 8x8 chessboard in the square
    window defined by WIDTH and HEIGHT"""
    global WIDTH, HEIGHT

    # compute size of square 
    side = WIDTH//8

    # display 8 columns and 8 rows of squares
    for i in range( 0, 8 ):
        for j in range( 0, 8 ):

            # create a rectangle
            r = Rectangle( Point( i*side, j*side ),
                           Point( i*side+side, j*side+side ) )

            # figure out if it should be black or white
            if (i+j) % 2 != 0:
                r.setFill( "grey" )
            else:
                r.setFill( "white" )

            # draw it
            r.draw( win )

def main():
    global WIDTH, HEIGHT

    # open a graphics window.
    win = GraphWin( "Chessboard", WIDTH, HEIGHT )

    # display the board.
    displayBoard( win )

    # wait for use to click window, and close up.       
    win.getMouse()
    win.close()

main()


Gifs for the game of Checkers

  • piece.gif

Piece.gif

  • piece2.gif

Piece2.gif


Interactive Checkers

InteractiveCheckers.png


# Interactive game of checkers.
# This program creates an 8x8 board, with
# alternating white and dark cells.  It then positions
# 3 rows of white pieces, and 3 rows of black pieces at
# the top and bottom of the board, respectively.
# It then allows the user to click on various pieces to make them 
# disappear.

from graphics import *

WIDTH = 600
HEIGHT = 600

class CheckersPiece:
    """ a class defining a checkers piece.  It uses gif images for
    each piece."""

    def __init__(self, color, i, j ):
        global WIDTH, HEIGHT
        side = WIDTH // 8
        x = i * side
        y = j * side
        centerPoint = Point( x+side//2, y+side//2 )
        if color=="white":
            self.img = Image( centerPoint, "piece2.gif" )
        else:
            self.img = Image( centerPoint, "piece.gif" )
            
    def draw( self, win ):
        self.img.draw( win )
        
    def undraw( self ):
        self.img.undraw()

    def getCoords( self ):
        global WIDTH, HEIGHT
        side = WIDTH // 8
        x = self.img.getAnchor().getX()
        y = self.img.getAnchor().getY()
        return x-side//2, y-side//2, x+side//2, y+side//2

def isWhite( i, j ):
    if (i+j)%2==0:
        return True
    return False
           
def displayBoard( win ):
    """this function displays the 8x8 board, with alternating
    colors."""
    global WIDTH, HEIGHT
    side = WIDTH//8
    for i in range( 0, 8 ):
        for j in range( 0, 8 ):
            r = Rectangle( Point( i*side, j*side ),
                           Point( i*side+side, j*side+side ) )
            if not isWhite( i, j ):
                r.setFill( "grey" )
            else:
                r.setFill( "white" )
            r.draw( win )

def createDisplayPieces( win ):
    """create 3 rows of white pieces at the top,
    and 3 rows of black pieces at the bottom.  Put them
    only on dark squares."""
    whites = []
    blacks = []
    for i in range( 8 ):
        for j in range( 3 ):
            if isWhite( i, j ):
                continue
            c = CheckersPiece( "white", i, j )
            c.draw( win )
            whites.append( c )
            
        for j in range( 5, 8 ):
            if isWhite( i, j ):
                continue
            c = CheckersPiece( "black", i, j )
            c.draw( win )
            blacks.append( c )

    return whites, blacks

def findClickedPiece( p, whites, blacks ):
    """loop through all the pieces and see if the point that the user
    clicked (p) is inside the square that corresponds to where the piece
    is."""
    for c in whites + blacks:
        x1, y1, x2, y2 = c.getCoords()
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            return c
    return None

def main():
    global WIDTH, HEIGHT
    
    win = GraphWin( "Chessboard", WIDTH, HEIGHT )

    displayBoard( win )
    whites, blacks = createDisplayPieces( win )

    # animation loop
    while True:
        p = win.getMouse()
        c = findClickedPiece( p, whites, blacks )
        if c != None:
            c.undraw( )
            
            
            
    win.getMouse()
    win.close()

main()


Mon, 11/16/15


Button Demo


# buttonDemo.py
# D. Thiebaut

from graphics import *
WIDTH = 600
HEIGHT = 600

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Pushbutton Demo", WIDTH, HEIGHT )

    button1 = Button( 10, 10, 60, 30, "OK" )
    button1.draw( win )
    
    while True:
        clickPoint = win.getMouse()
        if button1.isClicked( clickPoint ):
            print( "button clicked!" )

    win.close()

main()


Random Circles Demo


# randomDotDemo.py
# D. Thiebaut

from graphics import *
from random import *

WIDTH = 600
HEIGHT = 600

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def undraw( self ):
        self.frame.undraw( )
        self.label.undraw( )
        
    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def addDot( win ):
    x = randint( 100, 500 )
    y = randint( 100, 500 )
    center = Point( x, y )
    radius = randint( 10, 600 )
    c = Circle( center, radius )
    r = randint( 0, 255 )
    g = randint( 0, 255 )
    b = randint( 0, 255 )
    c.setFill( color_rgb( r, g, b ) )
    c.draw( win )
    
def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Pushbutton Demo", WIDTH, HEIGHT )

    # create UI
    OkButton = Button( 10, 10, 60, 30, "Ok" )
    OkButton.draw( win )
    quitButton = Button( 80, 10, 60, 30, "Quit" )
    quitButton.draw( win )
    addDotButton = Button( 150, 10, 60, 30, "Add Dot" )
    addDotButton.draw( win )
    
    # animation loop
    while True:
        clickedPoint = win.getMouse()
        if quitButton.isClicked( clickedPoint ):
            break
        
        if OkButton.isClicked( clickedPoint ):
            print( "button clicked!" )
            continue

        if addDotButton.isClicked( clickedPoint ):
            addDot( win )

            # redraw the buttons, just in case a circle covers them.
            OkButton.undraw()
            OkButton.draw( win )
            quitButton.undraw()
            quitButton.draw( win )
            addDotButton.undraw()
            addDotButton.draw( win )

            # go back to top of loop and wait for another click from user...
            continue
        
        # if we're here, it's because user
        # clicked on window, but not on buttons
        print( "clicked on window" )
        
    win.close()

main()


Polygon Demo


# buttonDemo.py
# polygon demo.  A triangle
# and a start are drawn
# D. Thiebaut

from graphics import *
from random import *
WIDTH = 300
HEIGHT = 300


def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Pushbutton Demo", WIDTH, HEIGHT )

    # draw a triangle
    points = [ Point( 100, 100 ),
               Point( 150, 100 ),
               Point( 175, 150 ) ]
    poly1 = Polygon( points )
    poly1.setFill( "orange" )
    poly1.draw( win )

    # draw a star
    points = [ Point( 35, 120 ),
               Point( 37.9,129.1),
               Point( 46.9,129.1),
               Point( 39.7,134.5),
               Point( 42.3, 143.1 ),
               Point( 35, 139 ),
               Point( 27.7, 143.1 ),
               Point( 30.3, 134.5 ),
               Point( 23.1, 129.1 ),
               Point( 32.1, 129.1 ) ]
    poly2 = Polygon( points )
    poly2.setFill( "lightgreen" )
    poly2.draw( win )
    
    # wait for user to click mouse to close window
    win.getMouse()
    win.close()

main()


Create Polygon With Mouse Demo: Skeleton Program


SmithLogo.gif
# recordShapeIntoPolygon.py
# D. Thiebaut
# Brings up a gif image in the window
# and creates 2 buttons.
# User can click on buttons to 1) start
# Recording a shape, and 2) stop recording
# it and displaying it as light-blue polygon.
# WARNING: the program, as is, is not safe and will crash
#   if the user start the program and clicks on a point that is not
#   inside a button...   The problem is with pointList...

from graphics import *

WIDTH = 400
HEIGHT = 400

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def main():
    global WIDTH, HEIGHT

    # create a graphic window
    win = GraphWin( "Record Polygon", WIDTH, HEIGHT )

    # put a logo of Smith in the middle of the window
    img = Image( Point(WIDTH//2, HEIGHT//2), "smithLogo.gif" )
    img.draw( win )

    # create 2 buttons
    startB = Button( 10, 10, 60, 30, "Start" )
    startB.draw( win )
    stopB = Button( 80, 10, 60, 30, "Stop" )
    stopB.draw( win )

 # animation loop
    while True:
        clickedPoint = win.getMouse()
        
        # quit?
        if exitB.isClicked( clickedPoint ):
            break

        # start recording polygon?
        if startB.isClicked( clickedPoint ):
            pointList = []
            continue

        # stop recording polygon?
        if stopB.isClicked( clickedPoint):
            poly = Polygon( pointList )
            poly.setFill( "cyan" )
            poly.draw( win )
            continue

        # if we're here, it's because
        # the user clicked somewhere outside
        # the buttons
        pointList.append( clickedPoint )

    win.getMouse()   
    win.close()

main()



Wed. 11/18/15


SavePolyToFile.py


# recordShapeIntoPolygon.py
# D. Thiebaut
# Brings up a gif image in the window
# and creates 2 buttons.
# User can click on buttons to 1) start
# Recording a shape, and 2) stop recording
# it and displaying it as light-blue polygon.

from graphics import *

WIDTH = 400
HEIGHT = 400
CSVFILE = "map.csv"

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def savePolyToFile( pointList, fileName ):
    file = open( fileName, "a" )

    L = []
    for point in pointList:
        x = point.getX()
        y = point.getY()
        #print( x, y )
        L.append( str( x ) ) # "{0:1}".format( x )
        L.append( str( y ) )
        
    #print( ", ".join( L ) )
    file.write( ", ".join( L )  )
    file.close()
    
def main():
    global WIDTH, HEIGHT, CSVFILE

    # create a graphic window
    win = GraphWin( "Record Polygon", WIDTH, HEIGHT )

    # put a logo of Smith in the middle of the window
    img = Image( Point(WIDTH//2, HEIGHT//2), "smithLogo.gif" )
    img.draw( win )

    # create 2 buttons
    startB = Button( 10, 10, 60, 30, "Start" )
    startB.draw( win )
    stopB = Button( 80, 10, 60, 30, "Stop" )
    stopB.draw( win )
    exitB = Button( 150, 10, 60, 30, "Quit" )
    exitB.draw( win )

    # create empty list of points
    pointList = []
    
    # animation loop
    while True:
        clickedPoint = win.getMouse()
        
        # quit?
        if exitB.isClicked( clickedPoint ):
            break

        # start recording polygon?
        if startB.isClicked( clickedPoint ):
            pointList = []
            continue

        # stop recording polygon?
        if stopB.isClicked( clickedPoint):
            poly = Polygon( pointList )
            poly.setFill( "cyan" )
            poly.draw( win )
            savePolyToFile( pointList, CSVFILE ) 
            continue

        # if we're here, it's because
        # the user clicked somewhere outside
        # the buttons
        pointList.append( clickedPoint )

        
    win.getMouse()        
    win.close()

main()


Different Button Class


class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked  # toggle
            
            #if self.clicked:
            #    self.frame.setFill( "yellow" )
            #else:
            #    self.frame.setFill( "white" )
            
            self.frame.setFill( "red" )
            sleep( 0.1 ) # 0.1 sec
            self.frame.setFill( "white" )
            
            return True
        else:
            return False


Fri. 11/20/15


Merge 2 Lists


# merging.py
# D. Thiebaut
# given 2 lists (in the form of stings), one a list
# of Disney characters, one a fictitious list of
# characters and their phone number, merge the
# two lists and print the name of the Disney characters
# and the area code they live in.

# master list
master = """Mickey Mouse, Mickey Mouse Clubhouse
Pluto, The Chain Gang
Minnie Mouse, Plane Crazy
Donald Duck, The Wise Little Hen
bambi, bambi
Pluto The Chain Gang
"""

# secondary list
phones = """Sue Green, (413) 585 1122, single
Alex Marquis, (617) 512 1234, married
Bambi, single
Mickey Mouse, (212) 123 5598, married
Pluto, (212) 123 5598, single
Minnie Mouse, (212) 123 5598, married
Donald Duck, (617) 525 1212, single
Bambi, (209) 713 1139, single
"""

def findPhone( name ):
    """given name, a stripped name of a Disney
    character, locate line in phone list, and
    return line that contains phone associated
    with name.
    """
    # split the string into lines
    phoneList = phones.split( "\n" )

    # look at each line and see if we find the
    # character with the given name
    for line in phoneList:
        
        # is it valid?  Does it contain commas?
        if len( line.split( "," ) ) != 3:
            # no
            continue

        # if we're here, we know the phone line is valid
        # make it lower case, and see if our name is included in it
        if line.lower().find( name ) != -1:

            # look for the position of the left parenthesis
            index = line.find( "(" )

            # return a slice of the string, containing the 3 area code digits
            return line[index+1:index+4]

    # if we didn't find the name in any of the lines,
    # return None, indicating "not found"
    return None    

def main():

    # split the master list into lines
    disneyList = master.split( "\n" )

    # for each line in the master list...
    for character in disneyList:

        # clean up the line
        character = character.strip()

        # if not a blank line, process it
        if len( character ) > 1:
            
            # get rid of lines not containing commas
            fields = character.split(",")
            if len( fields ) <= 1:
                continue

            # get the name
            name = fields[0].lower().strip()

            # look in secondary list, and fine
            # the line associated with name
            phoneLine = findPhone( name )

            # if we found a phone number, list it, otherwise
            # skip that Disney character...
            if phoneLine != None:
                print( name.title(), phoneLine )
            

main()


ColorOf


def colorOf( year ):
	# assume year is an int
	if year <= 1799:
		return 215, 97, 1
	# if we're here... year is
	# above 1800  
	elif year <= 1899:
		return 241, 163, 64
	elif year <= 1999:
		return 253, 184, 99
	else:
		return 254, 224, 144


Aquarium Skeleton Program

# Aquarium.py
# D. Thiebaut
# A program with fish swimming in an aquarium
# Two buttons control the creation of new fish, and how to
# quit the program.

from graphics import *
from time import sleep
from random import *

# Global variables
WIDTH = 700
HEIGHT = 517
AQUARIUM = "Tank2.gif"
FISH = "Fish10.gif"

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            self.frame.setFill( "red" )
            sleep(0.1)
            self.frame.setFill( "white" )
            return True
        else:
            return False

class Fish:
    """A class for a fish, which is an image
    """
    def __init__( self, p ):
        self.img = Image( p, FISH )

    def draw( self, win ):
        self.img.draw( win )

    def autoMove( self ):
        """makes the fish move. If the fish disappears from
        the right, it automatically reappears on the left."""
        dy = randint( -3, 3 )  # random vertical drift
        dx = randint( 0, 3 )   # random move forward
        self.img.move( dx, dy )

        # has it disappeared on the right?
        x = self.img.getAnchor().getX()
        if x > WIDTH + 50:
            # yes, move it to the left of the aquarium
            self.img.move( -WIDTH-50, 0 )

    
#---------------------------------------------------------------
# Functions
#---------------------------------------------------------------

def main():
    """The main interactive function.  Declare the graphic
    window, populate it with a map and 2 buttons, and allows
    the user to enter different polygons."""
    global WIDTH, HEIGHT, AQUARIUM

    # create graphic window.  make it the size of the map.
    win = GraphWin( "Aquarium", WIDTH, HEIGHT )

    # display background image
    map = Image( Point( WIDTH//2, HEIGHT//2 ), AQUARIUM )
    map.draw( win )

    # create 2 buttons to start recording and record points to
    # csv
    addFish = Button( 10, 10, 60, 30, "Add Fish" )
    addFish.draw( win )
    quitButton = Button( 80, 10, 60, 30, "Quit" )
    quitButton.draw( win )

    fishList = []
    
    # animation loop
    while True:
        # has user clicked on a point?
        clickedPoint = win.checkMouse()

        # yes, mouse clicked.
        if clickedPoint != None and quitButton.isClicked( clickedPoint ):
            break
        
        # was it on add-fish button?
        if clickedPoint != None and addFish.isClicked( clickedPoint ):
            clickedPoint = win.getMouse()
            fish = Fish( clickedPoint )
            fish.draw( win )
            fishList.append( fish )
            continue

        for fish in fishList:
            fish.autoMove()

        
    # We currently will not reach this point, as there is
    # no way to escape the while loop.  A good idear would be
    # to add an exit button.
    win.close()

main()



Aquarium.py (Fully functional)


# Aquarium.py
# D. Thiebaut
# A program with fish swimming in an aquarium
# Two buttons control the creation of new fish, and how to
# quit the program.

from graphics import *
from time import sleep
from random import *

# Global variables
WIDTH = 700
HEIGHT = 517
AQUARIUM = "Tank.gif"
FISH = "Fish10.gif"

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            self.frame.setFill( "red" )
            sleep(0.1)
            self.frame.setFill( "white" )
            return True
        else:
            return False

class Fish:
    """A class for a fish, which is an image
    """
    def __init__( self, p ):
        self.img = Image( p, FISH )

    def draw( self, win ):
        self.img.draw( win )

    def autoMove( self ):
        """makes the fish move. If the fish disappears from
        the right, it automatically reappears on the left."""
        dy = randint( -3, 3 )  # random vertical drift
        dx = randint( 0, 3 )   # random move forward
        self.img.move( dx, dy )

        # has it disappeared on the right?
        x = self.img.getAnchor().getX()
        if x > WIDTH + 50:
            # yes, move it to the left of the aquarium
            self.img.move( -WIDTH-50, 0 )

    
#---------------------------------------------------------------
# Functions
#---------------------------------------------------------------

def main():
    """The main interactive function.  Declare the graphic
    window, populate it with a map and 2 buttons, and allows
    the user to enter different polygons."""
    global WIDTH, HEIGHT, AQUARIUM

    # create graphic window.  make it the size of the map.
    win = GraphWin( "Aquarium", WIDTH, HEIGHT )

    # display background image
    map = Image( Point( WIDTH//2, HEIGHT//2 ), AQUARIUM )
    map.draw( win )

    # create 2 buttons to start recording and record points to
    # csv
    addFish = Button( 10, 10, 60, 30, "Add Fish" )
    addFish.draw( win )
    quitButton = Button( 80, 10, 60, 30, "Quit" )
    quitButton.draw( win )

    fishList = []
    
    # animation loop
    while True:
        # has user clicked on a point?
        clickedPoint = win.checkMouse()

        # yes, mouse clicked.
        if clickedPoint != None and quitButton.isClicked( clickedPoint ):
            break
        
        # was it on add-fish button?
        if clickedPoint != None and addFish.isClicked( clickedPoint ):
            clickedPoint = win.getMouse()
            fish = Fish( clickedPoint )
            fish.draw( win )
            fishList.append( fish )
            continue

        for fish in fishList:
            fish.autoMove()

        
    # We currently will not reach this point, as there is
    # no way to escape the while loop.  A good idear would be
    # to add an exit button.
    win.close()

main()


Mon. 11/23/15


Milou.gif

CountUniqueDogsWithLists.py


# countingUniqueDogsWithList.py
# D. Thiebaut
# Use a list to figure out how many unique
# dogs are in the text variable.

# a text string with many dogs.  Some are repeats.
text = """Fido 0
Fido 10
Rex 0
Milou 9
Ralph 3
Fido 8
"""

# MAIN:
def main():
    dogsFound = []
    # look at each line of text
    for line in text.split( "\n" ):

        # split the line on white space
        fields = line.capitalize().strip().split()

        # if invalid, skip
        if len( fields ) != 2:
            continue

        # get dog name
        name = fields[0]

        # have we seen it yet?
        if not name in dogsFound:
            dogsFound.append( name )


    print( "We have found", len( dogsFound ), "dog(s)" )
    print( "The dogs found are", ", ".join( dogsFound ) )

main()


CountingDogsWithDictionary.py


# countingUniqueDogsWithDictionary.py
# D. Thiebaut
# Demonstration program of the use of a dictionary
# to keep track of unique elements found in a collection
# of lines.

# the list of dogs, and some value associated with each one.
text = """Fido 0
Fido 10
Rex 0
Milou 9
Ralph 3
Fido 8
"""

def main():

    # create an empty dictionary
    dogsFound = {}

    # look at each line of text
    for line in text.split( "\n" ):

        # split the line on white space
        fields = line.capitalize().strip().split()

        # if invalid, skip
        if len( fields ) != 2:
            continue

        # get dog name
        name = fields[0]
        age = int( fields[1] )

        # have we seen it yet?
        dogsFound[ name ] = age

    print( "We have found", len( dogsFound ), "dog(s)" )
    print( "The dogs found are:" )

    for name in dogsFound.keys():
        print( name, dogsFound[ name ] )

main()


Keeping track of the first, last, and dogs with largest key found


# countingUniqueDogsWithDictionary.py
# D. Thiebaut
# Demonstration program of the use of a dictionary
# to keep track of unique elements found in a collection
# of lines.

# the list of dogs, and some value associated with each one.
text = """Fido 0
Fido 10
Rex 0
Milou 9
Ralph 3
Fido 8
"""

def main():
    dogsFoundFirst = {}
    dogsFoundLast = {}
    dogsFoundLargest = {}
    
    # look at each line of text
    for line in text.split( "\n" ):

        # split the line on white space
        fields = line.capitalize().strip().split()

        # if invalid, skip
        if len( fields ) != 2:
            continue

        # get dog name
        name = fields[0]
        age = int( fields[1] )

        # Keep the first dog we see
        if not name in dogsFoundFirst.keys():    
            dogsFoundFirst[ name ] = age

        # Keep the last dog we see
        dogsFoundLast[ name ] = age

        # Keep the dog with the largest associated value
        if not name in dogsFoundLargest.keys():
            dogsFoundLargest[ name ] = age
        else:
            # is the new dog found with an age larger than
            # what we have recorded so far, for this dog?
            if dogsFoundLargest[ name ] < age:
                # no, update and keep the new larger age
                dogsFoundLargest[ name ] = age

    print( "We have found", len( dogsFoundFirst ), "unique dog(s)" )
    
    print( "The first dogs found are:" )
    for name in dogsFoundFirst.keys():
        print( " -", name, dogsFoundFirst[ name ] )


    print( "\nThe last dogs found are:" )
    for name in dogsFoundLast.keys():
        print( " -", name, dogsFoundLast[ name ] )

    print( "\nThe dogs with the largest age found are:" )
    for name in dogsFoundLargest.keys():
        print( " -", name, dogsFoundLargest[ name ] )

main()


Nov. 30, 2015



...