试图让它成为最大的任务在打印中被指出。得到其他一切只需要能够抓住最大的任务。
def main ():
print("*** TIME MANAGEMENT ASSISTANT ***") NumberOfTasks = int(input("Number of tasks: "))
total = 0.0
TotalHour = 0.0
Day = 0.0
Minute = 0.0
EndTimeMinute = 0.0
EndTimeHour = 0.0
EndTimeDay = 0.0
RemainingHour = 0.0
AverageMinutes = 0.0print("What time will you start (on a 24-hour clock)? ")
StartHour = int(input("Hour: "))
StartMinute = int(input("Minute: "))
for i in range (NumberOfTasks):
TaskName = input("Task description: ")
TaskTime = int(input("How many minutes will this task take? "))
total = int(total + TaskTime)
TotalHour = int(total//60)
Minute = int(total%60)
Day = int(TotalHour//24)
RemainingHour = int(TotalHour%24)
EndTimeDay = Day
EndTimeHour = TotalHour + StartHour
if EndTimeHour > 24:
EndTimeHour = int(total%60)
EndTimeMinute = Minute + StartMinute
AverageMinutes = float(total//NumberOfTasks)
print("TOTAL TIME:", Day, "day(s)", RemainingHour, "hour(s)", Minute, "minute(s)")
print("END TIME: ", "In ", EndTimeDay, " day(s) at ", EndTimeHour, ":", EndTimeMinute,sep="")
print("LONGEST TASK: ")
print("AVERAGE TASK LENGTH:", round(AverageMinutes,2), "minutes")
答案 0 :(得分:0)
您需要记录每个“任务”的时间,然后通过排序来计算循环后的最长时间。要实现这种方式,您需要确保任务名称是唯一的,因此我添加了一行代码来做到这一点(所有添加的内容均已注释):
def main():
print("*** TIME MANAGEMENT ASSISTANT ***")
NumberOfTasks = int(input("Number of tasks: "))
total = 0.0
TotalHour = 0.0
Day = 0.0
Minute = 0.0
EndTimeMinute = 0.0
EndTimeHour = 0.0
EndTimeDay = 0.0
RemainingHour = 0.0
AverageMinutes = 0.0
print("What time will you start (on a 24-hour clock)? ")
StartHour = int(input("Hour: "))
StartMinute = int(input("Minute: "))
# create a dictionary to store time for each task
task_times = {}
for i in range (NumberOfTasks):
TaskName = input("Task description: ")
# ensure task name is unique
while TaskName in task_times.keys():
TaskName = input("Task description has already been used. Please enter a unique description: ")
TaskTime = int(input("How many minutes will this task take? "))
total = int(total + TaskTime)
TotalHour = int(total//60)
Minute = int(total%60)
Day = int(TotalHour//24)
RemainingHour = int(TotalHour%24)
EndTimeDay = Day
EndTimeHour = TotalHour + StartHour
if EndTimeHour > 24:
EndTimeHour = int(total%60)
EndTimeMinute = Minute + StartMinute
AverageMinutes = float(total//NumberOfTasks)
# record time for this task in the dictionary
task_times[TaskName] = TaskTime
print("TOTAL TIME:", Day, "day(s)", RemainingHour, "hour(s)", Minute, "minute(s)")
print("END TIME: ", "In ", EndTimeDay, " day(s) at ", EndTimeHour, ":", EndTimeMinute, sep="")
# calculate longest task by sorting by time and taking the last item
longest_task_name, longest_task_length = sorted(task_times.items(), key=lambda x: x[1])[-1]
print("LONGEST TASK: {}".format(longest_task_name))
print("AVERAGE TASK LENGTH:", round(AverageMinutes,2), "minutes")