Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

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.

Answer

There are three errors, plus one line that is missing.

  • The file name has no .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.
  • The opened file is not stored in a variable. open(...) on its own opens the file and then throws it away, so nothing later in the program can read from it.
  • The loop uses a variable that does not exist. 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.
  • The file is never closed. The program does not crash without it, but a program should close a file once it has finished with it. The .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?

Answer

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?

Answer

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?

Answer

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.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The file name is a string, in quotes, and ends in .txt.
  • The mode is "r", in quotes, after a comma.
  • The opened file is stored in a variable, and that same variable name is used in the for line and in the .close() line.
  • The 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.