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

In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.

The program below builds up a list of students in a text file. It asks for a name, adds it to the end of StudentInfo.txt, and then asks whether there is another one. Run it a few times: add one student and answer n, then add three and answer y twice before the n.

repeat = True

while repeat == True:
    print("Enter student name")
    student_name = input()
    student_name = student_name + "\n"

    data_file = open("StudentInfo.txt", "a")
    data_file.write(student_name)
    data_file.close()

    print("Enter another student? y/n")
    choice = input()

    if choice == "n":
        repeat = False
        print("Goodbye!")

What mode is the file StudentInfo.txt being opened in on line 8, and what does this specifically do to the file?

Answer

Append mode, the "a" in open("StudentInfo.txt", "a").

Append mode keeps everything that is already in the file and adds the new text after the last line. If the file does not exist yet, it is created first. So the first run of the program starts the file, and every run after that adds to the same list: nobody entered earlier is ever lost.

Change the "a" to "w" and the program still runs without an error, but every name wipes the one before it, so the file only ever holds the last student entered.

Why is \n concatenated to the end of the student_name variable on line 6 before it is written?

Answer

Because write does not start a new line by itself.

\n is the newline character. Adding it to the end of each name means every name finishes its own line, and the next name appended to the file starts on a new one. Without it, three students called Amira, Ben and Chloe end up as AmiraBenChloe, all on one line, and there is no way left to tell where one name stops and the next begins.

What is the purpose of the repeat variable initialised on line 1 and evaluated on line 3?

Answer

It is a flag that decides whether the loop keeps going.

repeat is a Boolean variable: it holds True or False. It starts as True on line 1, so the condition on line 3, repeat == True, is true the first time and the loop body runs. It stays True for as long as the user wants to add students. Line 16 is the only place it changes, to False, and the next time line 3 is evaluated the condition is false and the loop ends.

This is a condition controlled loop: nobody knows in advance how many students there will be, so the loop runs until the user says to stop.

If a user inputs y on line 13, which line of code will execute immediately after line 15?

Answer

Line 3.

With y as the answer, the condition on line 15, choice == "n", is false, so lines 16 and 17 are skipped. That was the last part of the loop body, so the program goes back to the top of the loop and evaluates line 3 again. repeat is still True, so the loop runs once more, starting from line 4 with the next student’s name.

What logic error would occur if line 10 was accidentally deleted from the program?

Answer

The file would be opened on every repetition and never closed.

The program would not crash, which is what makes it a logic error rather than a syntax error. But write does not always put the text into the file straight away: Python can hold it in memory and save it later, and close is what makes sure it is saved. Without it, a name the user has already typed might not be in the file yet, for instance if the program is stopped halfway, and another program that opens StudentInfo.txt in the meantime could find it incomplete.

Python often tidies up after you, so the names may well turn up in the file anyway. That is exactly what makes this kind of mistake hard to spot: the program looks as if it works until the day it does not.

How does the condition on line 15 directly control how many times the loop will run?

Answer

It is the only way out of the loop.

Line 15 checks whether the answer is exactly n. Only then does line 16 set repeat to False, which ends the loop. Any other answer leaves repeat as True, so the loop runs again. That means the loop runs once for every student, and the number of repetitions is decided by the user, one answer at a time.

It also means that N, no or a typing mistake all count as “yes, another one”, because none of them is exactly n. The program never checks that the answer was y, only that it was not n.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The mode is "a" when the old contents have to stay, and "w" only when they may be thrown away.
  • Every line you write ends with "\n".
  • Every open has a matching close.
  • True and False start with a capital letter and have no quotes around them.
  • The line that changes the flag to False is inside the loop, or the loop never ends.
  • The answer is compared with ==, and the comparison is exact: N is not n.