jeudi 26 novembre 2020

Ask questions with attributes and functions

This is my code:

class People:

    def __repr__(self):
        return f"A('{self.name}', {self.age})"

    def __init__(self, name, age):
        self.name = name
        self.age = age

def sortlist():
    people = []
    file = open('people.txt', 'r').readlines()

    for k in file:
        k = k.split()
        people.append(People(k[0], float(k[1])))
    people.sort(key=lambda c: (c.age, c.name))
    g = 0
    for g in range(len(people)):
        people[g].height = g + 1
return people

def question_height():
    for i in sortlist():
            print(random(i[0]))

def question_name():
    for i in sortlist():
        for i in range(0,104):
            print(random.choice(i.name))

The last part of the code (after return people) does not work the way I want it to. I want the question_height function to pick out a random number from the height attribute and the question_name function to pick out a random name from the name attribute.

Does anyone have an idea on what I could do to make it work?




Aucun commentaire:

Enregistrer un commentaire