Chapter 18 Applied Python Exercise 7

Reminder: you will turn in your final script from this chapter by emailing it to your TA Kate Weaver (). Please name it LASTNAME_tail.py (replacing “LASTNAME” with your last name).

18.1 Goal – Print the desired number of lines from the end of the input file

The goal of this chapter is to edit your recreated head program from the fifth Applied Python Exercise chapter such that it tails an input file. As such, it should print the desired number of lines from the end of the input file, rather than from the beginning.

18.2 Learning Objectives

After going through this chapter, students should be able to:

  • State the sub-steps needed to meet the coding goal
  • Use the following datatypes, structures, and fundamentals to meet the coding goal:
    • importing modules
    • variable assignment
    • list indexing
    • list length
    • integer conversion
    • opening a file
    • list initialization
    • for loop
    • appending to a list
    • list slicing
    • logical expression
    • conditional statement
    • printing output

18.3 Coding Blueprint

We recommend that you start with the final pseudocode from the fifth Applied Python Exercise chapter:

First, we need to SET the input file
Next, IF the user-specified a desired number of lines to display
   THEN we need to SET the desired number of displayed lines
END IF
OTHERWISE
   THEN we need to SET the desired number of displayed lines to a default
END OTHERWISE
Then, FOR every line in the open file
   IF a desired line (by its numerical position)
     PRINT the line
   END IF
END FOR

Edit this pseudocode such that it reflects what you want to do. Keep lines that are still applicable, remove lines that are no longer applicable, add lines that you need.

Consider that two separate for loops could be useful for this task. One that extracts and stores every line from the file and then one that prints every line from a subset of those that you’ve stored with the first loop.

We’ve provided some pseudocode with blanks that we think will be helpful for you to fill out if you’re unsure of where to start or next steps:


Pseudocode with blanks:

First, we need to SET the input file
Next, IF the user-specified a desired number of lines to display
   THEN we need to SET the desired number of displayed lines
END IF
OTHERWISE
   THEN we need to SET the desired number of displayed lines to a default
END OTHERWISE
___ a storage list for lines in the file
Then, FOR every line in the open file
   IF a desired line (by its numerical position)
     PRINT the line
   END IF
   ___ the line to the storage list
END FOR

___ a subset of the storage list to be the last ____ items in the storage list
___ every line in the subset
   _____ the line
END FOR


18.4 Building the Code

Create a new Python script

Much like how we used the base pseudocode from the fifth Applied Python Exercise chapter, start with the final code that you have from the fifth Applied Python Exercise chapter. Reuse lines that are applicable to the task at hand and write new code for the new parts of the program that you specified in your new pseudocode. Consult the learning objectives above to see what data types, structures, and fundamentals we think you will need to build the code. Also, consult the notes on Python fundamentals. Remember to indent when appropriate.

Use the command line or the Run button in the online interface to run the Python script and look at the output.

Submit your completed script