Mój kod to: Wolontariusz klasy: Def __init__ (jaźń, nazwa, wiek, typ, hourcontribution): Self .__ Nazwa = Nazwa sam .__ Wiek = Wiek Self

0
Yew Loong 27 marzec 2021, 11:03

1 odpowiedź

Najlepsza odpowiedź

Proszę bardzo:

class volunteer:
    def __init__ (self, name, age, type, hourContribution):
        self.__name = name
        self.__age = age
        self.__type = type
        self.__hourContribution = hourContribution

    def getHourContribution (self):
        return self.__hourContribution

class volunteerList:
    def __init__ (self, groupName):
        self.vList = []
        self.__groupName = groupName

    def highestHour (self):
        highHourList = []
        hourList = []
        for volunteer in self.vList:
            hourList.append(volunteer.getHourContribution())
        highest = max(hourList)
        for hour in hourList:
            if hour == highest:
                highHourList.append(hour)
        print(highHourList)

new = volunteerList("one")
vol1 = volunteer("", 1, "", 5)
vol2 = volunteer("", 1, "", 10)
vol3 = volunteer("", 1, "", 10)
vol4 = volunteer("", 1, "", 10)
vol5 = volunteer("", 1, "", 1)
new.vList = [vol1, vol2, vol3, vol4, vol5]
new.highestHour()

Alternatywna funkcja najwyższej godziny

def highestHour (self):
    highHourList = []
    largest = self.vList[0].getHourContribution()
    for volunteer in self.vList:
        hour = volunteer.getHourContribution()
        if hour == largest:
            highHourList.append(hour)
        elif hour > largest:
            highHourList = [hour]
            largest = hour
    print(highHourList)

Potrzebuje sprzątania, ale masz pomysł.

0
ObjectJosh 27 marzec 2021, 08:41