Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

file reading coding challenge

could anybody help me with this challenge, please?
The surname and name of the students of the class and their grade for the control are written line by line into the text file. Display all students whose grade is less than 3 points and calculate the average grade for the class.

Example of file content:

https://drive.google.com/file/d/1zIWw4cYc8E_osILmTLnzxtGZ-ZlK4m8R/view?usp=sharing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

some instuctions
Before the contents of the file are read, two variables must be set to zero. One of them will accumulate the amount of points, and the other – the number of lines (number of people). The average score is found by dividing the sum by the quantity.
After the file is opened, it must be read line by line. From each line it is necessary to “cut out” an assessment. This is the last or penultimate character of the string. If the score is less than 3, then display the entire line on the screen.
The resulting score from each line must be converted to an integer and added to the sum, and when reading each line, increase the person counter by 1.
After the end of the file is reached, it must be closed, the average score calculated and displayed on the screen.

>Solution :

Here is my answer

student_data = {}
with open("test.txt", 'r') as f:
    data = f.readlines()
    for line in data:
        line_list = line.strip().split()
        student_data[line_list[0] + ' ' + line_list[1]] = int(line_list[2])

print("Students with grade less than 3 : ")
for key, value in student_data.items():
    if value < 3:
        print(key)

print(f'Average grade of class : {sum(student_data.values()) / len(student_data)}')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading