python - 我的 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
解决方案
为了做到这一点,如果您更改数据结构并使用 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]))
推荐阅读
- javascript - 如何使用 Pug 将 Markdown 文件目录转换为 HTML 页面?
- angular - Angular:img src 中的“警告:清理不安全的 URL 值”
- mongodb - Clojure - 如何在单元测试中正确挂载状态
- android - Android Studio Darcula 主题错误
- javascript - 带有字符“\”的 json 错误(意外标记)
- javascript - 模拟显示为未定义
- javascript - 期望 {} 是 chai 中的一个对象
- sql - 如何在 SSIS 的同一张表中从不同级别获取父子记录?
- node.js - 使用 Node JS 反应路由器
- python - XML findall 在第一次找到时停止