python-3.x - 此代码打印月份及其高温和低温的列表。如何使用更新功能?检查代码下的解释
问题描述
def main():
#temperature data for Aurora:
highTemps = [-3, -2, 3, 11, 19, 23, 26, 25, 20, 13, 6, 0]
lowTemps = [-11, -10, -5, 1, 8, 13, 16, 15, 11, 5, -1, -7]
weatherDB = createDB(highTemps, lowTemps)
for m in weatherDB:
for t in weatherDB[m]:
print(m, t, weatherDB[m][t])
m = input("Enter a Month Name: ")
if m in weatherDB:
print(weatherDB[m])
else:
print("Month not found")
'part 2'
def tempByMonth(weatherDB, month):
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]
weatherDB = {}
for i in range(len(months)):
month = months[i]
weatherDB[month]
return weatherDB
'part 4'
def update(weatherDB):
#complete this code according to the assignment instructions
update = input("What would you like to update: ")
newhigh = input("What new high temperature do you want: ")
newlow = input("What new low temperature do you want: ")
return
#DO NOT change this function:
def createDB(highTemps, lowTemps):
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]
weatherDB = {}
for i in range(len(months)):
month = months[i]
weatherDB[month]={"high":highTemps[i],"low":lowTemps[i]}
return weatherDB
main()
此代码打印月份及其高温和低温的列表。我如何使用更新功能来询问他们想要什么新的和低温,并打印一个包含更新月份的新列表?我已经输入了他们想要更新的月份以及他们想要的新的和低温的输入,但我不知道如何打印出相同的月份和温度列表,但使用更新的列表。
解决方案
只需读取输入直到正确:
def str_is_int(inp_str: str) -> bool:
try:
int(inp_str)
except ValueError:
return False
return True
def update(weatherDB: weather_db_type) -> None:
month: str = ""
while month not in MONTHS:
month = input("What would you like to update: ")
new_high_raw: str = ""
while not str_is_int(new_high_raw):
new_high_raw = input("What new high temperature do you want: ")
new_low_raw: str = ""
while not str_is_int(new_low_raw):
new_low_raw = input("What new low temperature do you want: ")
weatherDB[month]["high"] = int(new_high_raw)
weatherDB[month]["low"] = int(new_low_raw)
完整的代码,重新排列和输入:
from typing import Tuple, Dict
MONTHS: Tuple[str, ...] = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec",)
weather_db_type = Dict[str, Dict[str, int]]
def create_db(highTemps: Iterable[int], lowTemps: Iterable[int]) -> weather_db_type:
weather_db: weather_db_type = {}
for i in range(len(MONTHS)):
month = MONTHS[i]
weather_db[month] = {"high":highTemps[i], "low":lowTemps[i]}
return weather_db
def str_is_int(inp_str: str) -> bool:
try:
int(inp_str)
except ValueError:
return False
return True
def update(weatherDB: weather_db_type) -> None:
month: str = ""
while month not in MONTHS:
month = input("What would you like to update: ")
new_high_raw: str = ""
while not str_is_int(new_high_raw):
new_high_raw = input("What new high temperature do you want: ")
new_low_raw: str = ""
while not str_is_int(new_low_raw):
new_low_raw = input("What new low temperature do you want: ")
weatherDB[month]["high"] = int(new_high_raw)
weatherDB[month]["low"] = int(new_low_raw)
def main() -> None:
#temperature data for Aurora:
high_Temps = [-3, -2, 3, 11, 19, 23, 26, 25, 20, 13, 6, 0]
low_temps = [-11, -10, -5, 1, 8, 13, 16, 15, 11, 5, -1, -7]
weather_db = create_db(high_Temps, low_temps)
for m in weather_db:
for t in weather_db[m]:
print(m, t, weather_db[m][t])
m = input("Enter a Month Name: ")
if m in weather_db:
print(weather_db[m])
else:
print("Month not found")
main()
推荐阅读
- swift - 快速的语法。什么是
在“withUnsafeMutableBytes (_..." - pyspark - 是否可以在 2 个以上的流之间共享 foreachBatch 的 batchId?
- python - 为 UNET 编写特定的损失函数
- github - 如何在 Github Actions 中设置 docker 用户
- c - 无论使用什么数据大小进行分配,数组大小都初始化为 8
- javascript - Jest 无法使用 js 文件中的 HOC,但可以正确使用 tsx 文件中的 HOC
- c# - 专注于我的表单窗口,直到输入 C#
- php - Laravel:如何在一个控制器中访问两种不同模型的数据?
- html - 使用最佳实践 angular 7 显示当前时间 - 小时和分钟
- mongodb - Symfony 4 表单上的文件限制不起作用