Python 1: Introduction to Python
Homework 8

Due

Sunday

Deliverables

There is one deliverable for this assignment

Specification

The file temps_max_min.txt has three pieces of information on each line The file looks like this
2017-06-01 67 62
2017-06-02 71 70
2017-06-03 69 65
...
Your script will read in each line of this file and calculate the average temperature for that date using a function you create named average_temps.
Your script will find the date with the highest average temperature and the lowest average temperature.

average_temps

This function must have the following header
def average_temps(max, min):
This function will convert it's two parameters into integers and return the rounded average of the two temperatures.

Suggestions

Write this code in stages, testing your code at each step
  1. Create the script hw8.py and make it executable.
    Create a file object for reading on the file temps_max_min.txt.
    Run the script.
    You should see nothing.
    Fix any errors you find.
  2. Write for loop that prints each line in the file.
    Run the script.
    Fix any errors you find.
  3. Use multiple assignment and the split method on each line to give values to the variables date, max and min.
    Print these values.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement in the for loop.
    Create the function average_temps.
    Use this function inside the loop to calculate the average for each line.
    Print date, max, min and average.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement in the for loop.
    Now you need to create accumulator variables above the for loop.
    Create the variables max_average , min_average max_date and min_date.
    Assign max_average a value lower than any temperature.
    Assign min_average a value higher than any temperature.
    Assign the other two variables the empty string.
    Run the script.
    Fix any errors you find.
  6. Write an if statement that checks whether average is greater than the current value of max_average.
    If it is, set max_average to average and max_date to date.
    Outside the for loop print max_date and max_average .
    Run the script.
    Fix any errors you find.
  7. Write an if statement that checks whether average is less than the current value of min_average.
    If it is, set min_average to average and min_date to date.
    Outside the for loop print min_date and min_average .
    Run the script.
    Fix any errors you find.
  8. Change the print statement after the for loop so they look something like the output below.

Output

When you run your code the output should look like this
Maximum average temperature: 86 on 2017-06-12
Minimum average temperature: 63 on 2017-06-26