Home > Software design >  I'm trying to determine the min value in my array, but I keep getting traceback
I'm trying to determine the min value in my array, but I keep getting traceback

Time:06-24

Can someone help me identify where I went wrong with this code? I'm trying to determine the min value in my array. I collect student name, number and mark and am looking for the lowest mark.

def my_array():
 students = list()
 total = 0
 
 NumStudents = int(input("Please enter number of students: "))
 for i in range(int(NumStudents)):
   Student_Name = input("Please enter student name: ")
   students.append(Student_Name)
   Student_Num = int(input("Please enter student number: "))
   students.append(int(Student_Num))
   Student_Mark = int(input("Please enter student mark: "))
   students.append(int(Student_Mark))
   total  = Student_Mark
   listA = students
   new_list = np.array_split(listA, NumStudents) 
   for item in new_list:
     print(list(item))
     #print(list(item[0]))
   print('Average Student Mark is', str(total / NumStudents))
 def find_min(students):
   minimum = students[0]
   for x in students[1:]:
     if x < minimum:
       minimum = x
       return minimum
 find_min(students)
my_array()

but I keep getting traceback:

Traceback (most recent call last):
  File "C:\Users\MMaseko\PycharmProjects\utopiaWasteDisposalPlant\iterable.py", line 37, in <module>
    my_array()
  File "C:\Users\MMaseko\PycharmProjects\utopiaWasteDisposalPlant\iterable.py", line 36, in my_array
    find_min(students)
  File "C:\Users\MMaseko\PycharmProjects\utopiaWasteDisposalPlant\iterable.py", line 33, in find_min
    if x < minimum:
TypeError: '<' not supported between instances of 'int' and 'str'

CodePudding user response:

Some of the values in your list are strings (student names) and some are integers (students' grades). You can't find a minimum if some of the values you are comparing aren't integers.

def my_array():
    students = []
    total = 0
    n = int(input())
    for _ in range(n):
        name = input()
        number = int(input())
        mark = int(input())
        total =mark
        students.append((mark, number, name))
    return min(students)[2]

This solves your problem. There are some other parts of your code that you have, but I haven't implemented them because you didn't require it.

CodePudding user response:

Your find_min function is not good. I mean if there is no element lower than the first one, it won't return anything. But even if there is one - all the others won't be considered at all - the return statement will break the loop. The rigth way:

def find_min(students):
    minimum = students[0]
    for x in students[1:]:
        if x < minimum:
            minimum = x
    return minimum

CodePudding user response:

Here's a code that works, The find_min() function had an error due to an indentation problem with the return minimum

def my_array():
      students = list()
      total = 0  
      NumStudents = int(input("Please enter number of students: ")) #resizing array to the number of students entered by user
      for i in range(int(NumStudents)): #add student info to the empty array (students = list() )
        Student_Num = int(input("Please enter student number: "))
        students.append(int(Student_Num))
        Student_Mark = int(input("Please enter student mark: "))
        students.append(int(Student_Mark))
        total  = Student_Mark
        listA = students
        new_list = np.array_split(listA, NumStudents) #spliting array to number of students as entered by user
        for item in new_list:
          print(list(item))
          #print(list(item[0]))
        print('Average Student Mark is', str(total / NumStudents))
      def find_min(students): #determining lowest student mark
        minimum = students[0]
        for x in students[1:]:
          if x < minimum:
            minimum = x
        return minimum
      print("The lowest student mark is", str(find_min(students))) #printing lowest student mark
      find_min(students)
    my_array()
  • Related