查找列表中最高的小时 替代highestHour 函数

问题描述

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)

我不确定highestHour() 上的代码正确还是错误。我打算找到志愿者的最高小时数。如果志愿者的最高小时数(同一小时)超过 1 个,则显示所有内容。我的输出只是志愿者的最高一小时,或者显示 2 行相同的语句而不是之前的示例。

想知道如何显示所有最高小时的志愿者? 显示示例将是: a - 36 岁 - 大流行志愿者 b - 25 岁 - 洪水志愿者

解决方法

给你:

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("",10)
vol3 = volunteer("",10)
vol4 = volunteer("",10)
vol5 = volunteer("",1)
new.vList = [vol1,vol2,vol3,vol4,vol5]
new.highestHour()

替代highestHour 函数

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)

需要一些清理,但你懂的。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...