【问题标题】:How can I call dictionary for entered key by user?如何为用户输入的键调用字典?
【发布时间】:2021-01-03 16:21:10
【问题描述】:

我想拨打给定号码的月份。如果用户输入 1 等,我希望输出类似于“一月份的天数为 30”。

 while True:
 month_dict = {
    "1": "January",
    "2": "February",
    "3": "March",
    "4": "April",
    "5": "May",
    "6": "June",
    "7": "July",
    "8": "August",
    "9": "September",
    "10": "October",
    "11": "November",
    "12": "December" 
    }

month = int(input("Enter the number of month:"))

def number_of_date(month):
    for value in month_dict.items():
        if month in [1, 3, 5, 7, 8, 10, 12]:
            print("Number of days in", value, "is 31!!!")
        elif month in [4, 6, 9, 11]:
            print("Number of days in", value, "is 30!!!")
        elif month == 2:
            print("Number of days in", value, "is 28!!!")
        else:
            print("There is not a month like that.. Check your writing!")

number_of_date(month)        

【问题讨论】:

    标签: python-3.x function dictionary monthcalendar


    【解决方案1】:

    给你。只需要一点重新排序和修复。基础很扎实。

    month_dict = {
        1: "January", # removed "" around the numbers, because inputs are immediately converted to ints.
        2: "February",
        3: "March",
        4: "April",
        5: "May",
        6: "June",
        7: "July",
        8: "August",
        9: "September",
        10: "October",
        11: "November",
        12: "December" 
        }
        
    def number_of_date(month): # removed for loop. You only need to print it once
        if month in [1, 3, 5, 7, 8, 10, 12]:
            print("Number of days in", month_dict[month], "is 31!!!") # month_dict[month] returns the correct name.
        elif month in [4, 6, 9, 11]:
            print("Number of days in", month_dict[month], "is 30!!!")
        elif month == 2:
            print("Number of days in", month_dict[month], "is 28!!!")
        else:
            print("There is not a month like that.. Check your writing!")
    
    
    while True: #while loop only around function call. No need to redefine functions every time
        month = int(input("Enter the number of month:"))
        number_of_date(month)  
    #notice that your program will never end. Maybe loop only while month != -1. So -1 can be used to exit the program.
    

    【讨论】:

    • 正确。一项改进是考虑leap 年... ;-) 当然,这不是 PO 的要求。还没有。
    【解决方案2】:

    month_dict的键声明为int

    主要问题是month_dict 中的键是字符串。如果将它们更改为整数:

    month_dict = {
        1: "January",
        2: "February",
        3: "March",
        4: "April",
        5: "May",
        6: "June",
        7: "July",
        8: "August",
        9: "September",
        10: "October",
        11: "November",
        12: "December" 
        }
    

    然后您可以简单地使用month_dict[month]获取与其编号相关联的月份名称。

    删除for 循环

    在函数中丢失for 将使其打印一个最终结果。

    while 中删除month_dictnumber_of_date(month) 的声明

    最后,还要考虑这样做以避免一遍又一遍地声明相同的值。

    这是完整的代码:

    month_dict = {
        1: "January",
        2: "February",
        3: "March",
        4: "April",
        5: "May",
        6: "June",
        7: "July",
        8: "August",
        9: "September",
        10: "October",
        11: "November",
        12: "December" 
        }
    
    
    
    def number_of_date(month):
        if month in [1, 3, 5, 7, 8, 10, 12]:
            print("Number of days in", month_dict[month], "is 31!!!")
        elif month in [4, 6, 9, 11]:
            print("Number of days in", month_dict[month], "is 30!!!")
        elif month == 2:
            print("Number of days in", month_dict[month], "is 28!!!")
        else:
            print("There is not a month like that.. Check your writing!")
    
    while True:
        month = int(input("Enter the number of month:"))
        number_of_date(month)
    

    我想就是这样!
    如果您想检查下一个闰年,请参阅this tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      相关资源
      最近更新 更多