Search code examples
pythonlistpython-class

finding the highest hour in a list


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

    def setGroupName (self, newGroupName):
        self.__groupName = newGroupName

    def getGroupName (self):
        return self.__groupName

    def getvList (self):
        return self.__vList

    def addVolunteer (self, volunteer):
        self.getvList().append(volunteer)


    def highestHour (self):
        details = ""
        highestHourList = []
        highest = self.__vList[0].getHourContribution()
        for volunteer in self.__vList:
            hour = volunteer.getHourContribution()
            if hour == highest:
                highestHourList.append(hour)
            elif hour > highest:
                highestHourList = [hour]
                highest = hour
                #highestHourList.append(hour)
        if volunteer.getType().lower() == "f":
            details = details + "{} - {} years old - flood volunteer".format(volunteer.getName(), volunteer.getAge()) + "\n"
        elif volunteer.getType().lower() == "p":
            details = details + "{} - {} years old - pandemic volunteer".format(volunteer.getName(), volunteer.getAge()) + "\n"
        elif volunteer.getType().lower() == "e":
            details = details + "{} - {} years old - earthquake volunteer".format(volunteer.getName(), volunteer.getAge()) + "\n"
        elif volunteer.getType().lower() == "t":
            details = details + "{} - {} years old - tsunami volunteer".format(volunteer.getName(), volunteer.getAge()) + "\n"

        return details
def main ():
    groupName = input("Enter your group name: ")
    newGroup = volunteerList(groupName)
    print ("\n")

    choice = menu()

    while choice != "0":
        if choice == "1":
            name = input("Name of volunteer? ")
            age = int(input("Age? "))
            volunteerType = input("Type of volunteer ('F/P/E/T'): ")
            volunteerType = volunteerType.lower()
            while volunteerType not in "fpet":
                print ("Invalid type! Please enter again!")
                volunteerType = input("Type of volunteer ('F/P/E/T'): ")
                volunteerType = volunteerType.lower()

            hourCont = int(input("Contribution hour? "))
            while hourCont <= 0:
                print ("Invalid value! Please enter again!")
                hourCont = int(input("Contribution hour? "))

            newGroup.addVolunteer(volunteer(name, age, volunteerType, hourCont))
            print ("... Volunteer has been added successfully.")
            print ("\n")
            choice = menu()

        elif choice == "6":
            print ("Volunteer with highest contribution hour:")
            print (newGroup.highestHour())
            print ("\n)

I'm not sure the code on highestHour() correct or wrong. I was planning to find the highest hour of the volunteer(s). If there are more than 1 highest hour of volunteer (same hour), display everything. My output was only one highest hour of volunteer or display 2 same line of statement instead of the example before.

Wondering how to display all volunteer that are highest hour? The display sample will be: a - 36 years old - pandemic volunteer b - 25 years old - flood volunteer


Solution

  • Here you go:

    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()
    

    Alternative highestHour function

    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)
    

    Needs some cleaning up, but you get the idea.