【发布时间】: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