In an investigate task you take code apart to see how each piece contributes to the result. Answer every question for yourself first. Only then open the answer.
The program below is meant to open singers.txt, the file of eleven names from
the predict task, and print every line in it. It contains some common errors.
open("singers", "r")
for line in my_file:
print(line)
Find the errors, fix them, and write a comment next to each fix to explain what you did.
Try it in writing first: copy the code, correct it, and add a # comment to
every line you changed. The code on this page cannot open files, so to run your
version, use the To sandbox button of the Modify exercise in this
chapter, whose sandbox comes with singers.txt.
There are three errors, plus one line that is missing.
.txt. The file is called singers.txt, and the
extension is part of its name. open("singers", "r") looks for a file called
just singers, finds none, and stops the program with a FileNotFoundError.open(...) on its own opens
the file and then throws it away, so nothing later in the program can read
from it.for line in my_file:
reads from my_file, but nothing ever created my_file. Fix the first line
and the program would still stop here, with a NameError. Storing the file in
my_file fixes both errors at once..close() goes
after the loop, not indented.One corrected version:
my_file = open("singers.txt", "r") # added .txt and stored the file in my_file
for line in my_file: # my_file now exists, so the loop can read from it
print(line)
my_file.close() # added: close the file once the loop has finished
It prints the eleven names with a blank line after each one, just like the first predict example.
The remaining questions are about the corrected version.
What is the meaning of the "r" on line 1?
It is the mode the file is opened in, and "r" stands for read.
The program is only going to read what is in the file, not change it. Other letters open a file for writing instead, which is the topic of the next chapter.
What type of iteration is the for loop on line 3?
Count controlled iteration.
The loop repeats once for every line in the file, so the number of repetitions
is fixed before the loop starts: eleven lines, eleven repetitions. The program
never has to check a condition to decide whether to go round again, just as a
loop over a range does not.
It does not matter that the programmer does not know how many lines there are. The file does, and a longer file simply gives more repetitions.
The variable identifier line is changed to x. What impact does this have
on the code?
It depends on whether you change it everywhere.
Change it in both places, for x in my_file: and print(x), and nothing
changes at all. line is just a name the programmer chose, like the counter in
a range loop, and the program prints the same eleven names. The name line is
only a better choice because it says what the variable holds.
Change it only in the for line and the program breaks: print(line) now
uses a variable that no longer exists, so the program stops with a
NameError on the first repetition.
Make sure that you check for the following things:
.txt."r", in quotes, after a comma.for line and in the .close() line.for line has a colon (:) at the end, and the code to repeat is
indented under it..close() is not indented, so it runs once after the loop.