This is my code:
class People:
def __repr__(self):
return f"A('{self.name}', {self.age}, {self.height})"
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height
def sorted_list():
people = []
fil = open('peoples.txt', 'r').readlines()
for i in file:
i = i.split()
people.append(People(i[0], (i[1]))
people.sort(key=lambda a: (a.age, a.name))
My text file looks like this:
List:
Anna 13
Mark 44
James 80
Hanna 45
I want the attribute "height" to represent each persons position in the list using a for-loop. My code already sorts the people in the list according to their age, from youngest to oldest. Now I want the code to use a for-loop so that the attribute height gives each person from the sorted list their position in the list, i.e. gives Anna number 1, Hanna number 2 , Mark number 3 and James number 4.
This is what I've tried so far but it doesn't work:
i = 0
for height in people:
i += 1
people.height = int(i)
people = sorted(people)
return people
Does anyone have an idea for what I could do?