-1

I'm trying to find the min, max, and average of each student after(or within) the matrix. How would I get access to the list of each student's scores within the loop iteration? I've started with finding the minimum within the function(findlowest()) but can't figure out how to get just 1 student's exam scores at a time.

studentExam = 5
minMaxScore = 5

def main():
    global studentName, studentExam, score, scoreList, examScoreMaxList, min, max


def ValidateUser(score):
    while score < 0 or score > 100:
        score = float(input("Invalid input, please try again"))
    return score


def getStudentInfo():
    studentName = int(input("enter the number of student: "))
    # studentExam = int(input("how many exam scores: "))
    # Initialize matrix
    matrix = []
    # For user input
    for i in range(studentName):  # A for loop for row entries
        scoreList = []
        scoreList.append(input("enter name of students " + str(i + 1) + ": "))

        for j in range(studentExam):  # A for loop for column entries

            score = float(input("enter exam " + str(j + 1) + ": "))
            score = ValidateUser(score)
            scoreList.append(score)
        matrix.append(scoreList)
    print(matrix)
    # for printing
    for i in range(studentName):
        for j in range(studentExam+1):
            print(matrix[i][j], end=" ")
        print()
getStudentInfo()


def findLowest():
    minlist = []
    min = minlist[0]
    for i in studentExam[0:5]:
        if i < min:
            min = i
    print("the minimum number is: ", min)

findLowest()

I would like the code to display something similar to the following:

Mike H: min score - 78
        max score - 94
        avg score - 85

Sarah G: min score - 78
         max score - 94
         avg score - 85
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
Sirkel360
  • 1
  • 3
  • Please reduce and enhance this into the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. – Prune May 25 '20 at 22:08

2 Answers2

0

This should do it. You just needed a nested for loop.

scores = [["Mike", 100, 98, 43, 56, 43], ["John", 95, 32, 79, 75, 67]]

for arr in scores:
    #arr = ["Mike", 100, 98, 43, 56, 43] or ["John", 95, 32, 79, 75, 67]
    min = None
    max = None
    sum = 0
    studentName = None
    for i in arr:
        if type(i) != str:
            if not min or i < min:
                min = i

            if not max or i > max:
                max = i

            sum += i
        else:
            studentName = i
    avg = sum/5

    print(studentName + ": \t min score - " + str(min))
    print("\t max score - " + str(max))
    print("\t avg score - " + str(avg))
    print()
NumberC
  • 596
  • 7
  • 17
  • Hello Fadi Farag. Is there a way to do this without importing array? I keep getting the traceball error and namerror where score is not defined. Could you clarify (I'm a total rookie) how this gets the list from the matrix loop? Your answer was brilliant. Thanks much... – Sirkel360 May 25 '20 at 22:51
  • NameError: name 'score' is not defined. I did check my variables and played around with it – Sirkel360 May 25 '20 at 23:15
0

Mixing different types of data in a list can be confusing. I keep them separated - student name is the key in the dictionary, the scores list is value.

Also, regarding the first half of the sample code, a general best practice is to avoid using global variables, except for constant values.

def getStudentInfo():
    studentName = int(input("enter the number of student: "))
    # studentExam = int(input("how many exam scores: "))
    # Initialize matrix
    matrix = {}
    # For user input
    for i in range(studentName):  # A for loop for row entries
        name = input("enter name of students " + str(i + 1) + ": ")
        matrix[name] = []

        for j in range(studentExam):  # A for loop for column entries
            score = float(input("enter exam " + str(j + 1) + ": "))
            score = ValidateUser(score)
            matrix[name].append(score)
    print(matrix)
    return matrix
data = getStudentInfo()

def findInfo(data, name = None):
    def findInfoByName(name):
        if name in data:
            scores = data[name]
            minScore = min(scores)
            maxScore = max(scores)
            avgScore = sum(scores) / len(scores)
            print(f"{name}:\tmin score - {minScore}")
            print(f"\tmax score - {maxScore}")
            print(f"\tavg score - {avgScore}")
    if name is None:
        for name in data:
            findInfoByName(name)
    else:
        findInfoByName(name)

findInfo(data)
Kun Hu
  • 417
  • 5
  • 11
  • I'm new to python and wondering about your use of the "data = getStudentInfo()". Any suggestions on how to search to learn more. My search is simply resulting in data types, but nothing on how you used it. Thanks in advance... – Sirkel360 May 25 '20 at 23:47
  • @Sirkel360 you mean the use of "assignment"? This is a fundamental in most programming language, you probably want to go through some CS 101 or textbook. – Kun Hu May 25 '20 at 23:56