首页 > 解决方案 > 我的 python 程序的最后一部分有问题

问题描述

这里的所有代码都适用于我遇到的问题,我只需要使最后一行看起来像这样:

The highest rain total was Thursday with 6.54

代替:

Highest value is:  6.54

我意识到这是一件容易的事,但如果有人能快速帮助我,我将不胜感激。提前致谢

Sunday = float(input("Enter the rain totals for Sunday: "))
Monday = float(input("Enter the rain totals for Monday: "))
Tuesday = float(input("Enter the rain totals for Tuesday: "))
Wednesday = float(input("Enter the rain totals for Wednsday: "))
Thursday = float(input("Enter the rain totals for Thursday: "))
Friday = float(input("Enter the rain totals for Friday: "))
Saturday = float(input("Enter the rain totals for Saturday: "))

weekdays = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

print("Highest value is: ", max(weekdays))

index = 0

标签: pythonpython-3.xlist

解决方案


为了做到这一点,如果您更改数据结构并使用 adict而不是 a会更容易list,其键将是您提供的日期名称和值。然后,您可以找到最大值并将其与当天的名称一起打印:

weekdays = {'Sunday' : Sunday, 'Monday' : Monday, 'Tuesday' : Tuesday, 'Wednesday' : Wednesday, 'Thursday' : Thursday, 'Friday' : Friday, 'Saturday' : Saturday}
highestValue = max(weekdays, key=weekdays.get)
print("The highest rain total was " + highestValue + ' with ' + str(weekdays[highestValue]))

推荐阅读