54.6k views
0 votes
a text file has student data such that each line of text has a student first name (a non-empty alphabetic string) followed by exam scores (non-negative integers) separated by spaces. the number of scores might be different for each student, and each student has at least one score. see studentdata1, student data2, and student data3 in the drive folder homework. define a function student_minmax(filename) that has one parameter, the name of the file that has the student data, and returns a dictionary. the dictionary has the student names as keys and, as values, a list of two scores, the min and max scores for that student. for example, the call student_minmax('studentdata1.txt') will return the dictionary: {'joe': [5, 10], 'sue': [7, 10]}.

1 Answer

0 votes

Answer:

def student_minmax(filename):

"""

Description: Reads student names and finds min and max score

Input: Name of the file that has the student data

Output: Dictionary with name as key and value is a list with min and max values

"""

# Dictionary that holds the results

studentDict = {}

# Opening and reading file

with open(filename, "r") as fp:

# Reading data line by line

for line in fp:

# Stripping new line

line = line.strip()

# Splitting into fields

fields = line.split(' ')

# Getting name and scores

name = fields[0]

scores = []

scoresStr = fields[1:]

# Converting to integer

for score in scoresStr:

scores.append(int(score))

# Storing in dictionary

studentDict[name] = []

studentDict[name].append(min(scores))

studentDict[name].append(max(scores))

# Returning dictionary

return studentDict

# Testing function

resDict = student_minmax("d:\\Python\\studentMarks.txt")

# Printing dict

print(resDict)

Step-by-step explanation:

a text file has student data such that each line of text has a student first name-example-1
User Tedebus
by
6.0k points