Jan-24-2023, 01:30 PM
This is not meant to entirely answer your question, but rather to offer another option in the approach - more proof of concept.
When working with a set of related data, a Python class may be a useful construct. Dictionaries work great for paired data, but in this case you have a student name, rank, math and history scores.
The following defines a class that has those elements as well as a function that sums the math and history score. Recognize that there is no error checking for someone entering a score that is not an integer, etc, but again this is more showing the concept. I also have it just take 2 student entries. I get the rank by order of entry, which may not work but again, doing this to show another approach.
You access data and functions within a class using the dot (.)
When working with a set of related data, a Python class may be a useful construct. Dictionaries work great for paired data, but in this case you have a student name, rank, math and history scores.
The following defines a class that has those elements as well as a function that sums the math and history score. Recognize that there is no error checking for someone entering a score that is not an integer, etc, but again this is more showing the concept. I also have it just take 2 student entries. I get the rank by order of entry, which may not work but again, doing this to show another approach.
You access data and functions within a class using the dot (.)
class student: name = '' place = '' math = '' history = '' def total(self): return int(self.math)+int(self.history) students = [] # List of the students, will fill with student objects for count in range(2): # Loop to enter the student names and scores studlet = student() studlet.name = input('Enter Name ') studlet.place = count studlet.math = input('Enter Math Score ') studlet.history = input('Enter History Score ') students.append(studlet) # Append the student object to the list of students for student in students: # Loop to display the student object data print(student.name, student.place, student.math, student.history, student.total())
Output:Enter Name John
Enter Math Score 42
Enter History Score 38
Enter Name Mary
Enter Math Score 45
Enter History Score 30
John 0 42 38 80
Mary 1 45 30 75