【问题标题】:finding the highest hour in a list查找列表中的最高小时
【发布时间】:2021-06-24 00:10:45
【问题描述】:
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 岁 - 洪水志愿者

【问题讨论】:

  • 你能添加代码,在哪里创建这些类并调用它们的方法吗?另外,我猜def getHourContribution (self): 属于志愿者类,所以是不是缺少缩进?
  • 添加minimal reproducible example。不要让我们猜测输入什么,只需硬编码一些值并实例化您的类并重现错误
  • 抱歉没写好

标签: python list python-class


【解决方案1】:

给你:

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

替代的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)

需要一些清理,但你明白了。

【讨论】:

  • 如果我希望我的输出为:2 岁的海啸志愿者贡献了 6 小时。 b 2 岁是海啸志愿者,贡献了 6 小时。代替: b 2 岁的海啸志愿者已经贡献了 6 小时。 b 2 岁是一名海啸志愿者,贡献了 6 小时。
  • 如果您有更多问题,请更新您的代码并使用更新后的代码发布新帖子(即使它还没有工作)。在这篇文章中,您的问题是“在列表中查找最高小时”,我已经解决了这个问题。如果您的原始问题已得到解决,请将此帖子标记为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 2020-01-29
相关资源
最近更新 更多