如何为用户输入的键调用字典?

Posted

技术标签:

【中文标题】如何为用户输入的键调用字典?【英文标题】: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)        

【问题讨论】:

【参考方案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。

【讨论】:

以上是关于如何为用户输入的键调用字典?的主要内容,如果未能解决你的问题,请参考以下文章

Angular.js:如何为价格添加用户输入并保持小计

JavaScript 如何为同类用户数据创建基于Webix的多行输入

如何为用户输入生成的 mySQL 列添加添加安全性?

如何为任何输入添加带有快捷方式的特殊字符?

如何为用户输入文本时将动态创建的每个单元格设置按钮操作(Swift 4)?

如何为“其他 - 请指定”制作单选按钮?