[Solved] I tried BUT for some reason my data sorting is not aligned with student names and i am getting wrong grades opposite to wrong names [closed]


The problem is that when you sort the GPAs, they’re no longer associated with the name ordering – you can simply skip doing so and the list indicies will stay correct or bring them into some collection (the instructions actually specify a tuple)

# starting values
scores_GPA = [3.85, 4.89, 2.89, 1.02, 3.8, 3.7, 2.9, 2.5, 3.3, 3.5]
names = ["Sia", "Rihanna", "Taline", "Donald", "Sabra", "Stefanie", "Tome", "Jim", "Kalim", "Wu"]
# no need to sort

The instructions also likely mean to keep the values paired in a tuple, rather than having two individual tuples

simple tuple

>>> (names, scores_GPA)
(['Sia', 'Rihanna', 'Taline', 'Donald', 'Sabra', 'Stefanie', 'Tome', 'Jim', 'Kalim', 'Wu'], [3.85, 4.89, 2.89, 1.02, 3.8, 3.7, 2.9, 2.5, 3.3, 3.5])

tuple of tuples, maintaining the positions (likely intention)

>>> tuple(zip(names, scores_GPA))
(('Sia', 3.85), ('Rihanna', 4.89), ('Taline', 2.89), ('Donald', 1.02), ('Sabra', 3.8), ('Stefanie', 3.7), ('Tome', 2.9), ('Jim', 2.5), ('Kalim', 3.3), ('Wu', 3.5))

Now you can iterate by-items, getting pairs together!

>>> name_gpa_tuple = tuple(zip(names, scores_GPA))
>>> for name, gpa in name_gpa_tuple:  # NOTE this unpacks the inner tuple!
...    print(name, gpa)
...
Sia 3.85
Rihanna 4.89
Taline 2.89
Donald 1.02
Sabra 3.8
Stefanie 3.7
Tome 2.9
Jim 2.5
Kalim 3.3
Wu 3.5

Or more usefully for your example, some structure like this

max_GPA = -1
for name, gpa in name_gpa_tuple:
    if gpa > max_GPA:
        ...

You may also find creating a dictionary of the values is practical (as each name can be a unique key and provides a .items() method to iterate), but this steps outside the directions and may be marked extra for being clever or wrong on an assignment depending on the source’s policy
How do I sort a dictionary by value?

solved I tried BUT for some reason my data sorting is not aligned with student names and i am getting wrong grades opposite to wrong names [closed]