首页 > 解决方案 > 此代码打印月份及其高温和低温的列表。如何使用更新功能?检查代码下的解释

问题描述

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()

此代码打印月份及其高温和低温的列表。我如何使用更新功能来询问他们想要什么新的和低温,并打印一个包含更新月份的新列表?我已经输入了他们想要更新的月份以及他们想要的新的和低温的输入,但我不知道如何打印出相同的月份和温度列表,但使用更新的列表。

标签: python-3.x

解决方案


只需读取输入直到正确:

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()

推荐阅读