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
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:
test_scores.txt in read mode,while loop that keeps going as long as the line it read is not an
empty string (""),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.
One line, where N is the number of scores of 50 or more:
Number of students who passed: N
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.
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.
Make sure that you check for the following things:
.readline() before the loop and one at the end of the
loop’s body. Without the second one the loop never ends.line != "", with nothing at all between the two quotes.int() before it is compared with 50.>= 50, so that a score of exactly 50 is a pass.str() before it is joined onto the message.