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

Another make task, and this time the program has to read a whole file without knowing how long it is.

A teacher has a text file called test_scores.txt containing the exam results for a class. Every student’s score is on a line of its own, but the teacher does not know exactly how many students are in the file. It might look like this:

72
45
50
38
91
49
63

Your task

Write a program that uses a condition controlled loop to read through the entire file and count how many students achieved a passing score: 50 or more.

The program should:

The program asks the user nothing: everything it needs is in the file.

The tests also check your code: use a while loop to read the file.

The output

One line, where N is the number of scores of 50 or more:

Number of students who passed: N

Testing

Each test puts a different test_scores.txt next to your program before it runs, and the name of each test says what that file held. The files have different lengths, from a whole class down to a single student, and one of them is empty. An empty file has no scores in it, so nobody passed.

Example

With the seven scores shown above in test_scores.txt:

Output:

Number of students who passed: 4

72, 50, 91 and 63 are passes. 50 counts, since it is exactly the pass mark; 49 does not.

To try your program before you submit it, use the To sandbox button at the bottom of the editor. The sandbox comes with the test_scores.txt shown above.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The pass counter is created before the loop and given the value 0.
  • There is one .readline() before the loop and one at the end of the loop’s body. Without the second one the loop never ends.
  • The condition is line != "", with nothing at all between the two quotes.
  • Each line is wrapped in int() before it is compared with 50.
  • The comparison is >= 50, so that a score of exactly 50 is a pass.
  • The file is closed and the count printed after the loop, not indented.
  • The pass count is wrapped in str() before it is joined onto the message.