Write a program that can read the students records from the file “scores.dat”. This program will find the maximum and the minimum student scores. The first line in the file is a header. Each line (starting from the second line) contains the records of one student separated by spaces. These records are: the last name, the first, name, the ID, and the score.


def main():
infile = open(scores.dat, 'r')

# Finding the maximum

and now I do not know what to do?

Recommended Answers

All 5 Replies


import string

def main():
infile = open(scores.dat, 'r')

# Finding the maximum
for line in infile.readlines():
line = string.split(line)
score = line[3]
print score

i tried that but its not working.

Please use code tags when posting code.

Are you receiving an error message? If so, please post it.

Initialize a maximum score variable (maxscore). Loop on the file object for line in file_obj: . Strip and split each line lineList = line.strip().split() . If the value of the score is greater than maxscore, update maxscore and save the line in another variable.

An alternative would be to read all the lines, each stripped and split, into a list and sort the list.

Please use code tags for your code.

You are getting close, but you need to find if the score is a new minimum or maximum each time through the loop. Something like this:

infile = open("test_scores.dat", "r")
mylist = infile.readlines()

# remove the header, first item in the list
del mylist[0]

max_score = 0
min_score = 100
for line in mylist:
    # split each line into a list at space/whitespace and unpack
    last_name, first_name, ID, score = line.split()
    # for testing only
    print last_name, first_name, ID, score
    # create integer scores
    if score.isdigit():
        score = int(score)
        # check if it is a new max
        if score > max_score:
            max_score = score
        # check if it is a new min
        elif score < min_score:
            min_score = score

Then just print out the max_score and min_score results.

Please study the code and the comments!

can anyone help me to solve this problem as im still new to Python. Tq

  1. Read all the marks from the List, apply the following Grade rules and then display the mark and the respective grades as follow

mark for 23 students (75, 45, 89, 55, 65, 50, 72, 70, 60, 80, 95, 34, 83, 71, 65, 53, 58, 79, 85, 21, 67, 94,77)
0-49 - U
50-59- D
60-69 - C
70-79- B
80-89- A
90-100- A*

the output should be displayed as follows ;
Mark [0] - 75 - B
Mark [1] - 45 - U
Mark [2] - 89 - A

  1. Also count how many total number of grade A*, A grades, B grades, C grades, D grades and U grades and display on to the screen as follow:

Grade A* - 2
Grade A - 4
Grade B - 6

Please don't highjack existing threads. Create your own with the proper title. Also show some coding effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.