Python 3.x 如何为用户输入的密钥调用字典?
我想打一个月的电话号码。我希望输出像“1月的天数是30”,如果用户输入1等Python 3.x 如何为用户输入的密钥调用字典?,python-3.x,function,dictionary,monthcalendar,Python 3.x,Function,Dictionary,Monthcalendar,我想打一个月的电话号码。我希望输出像“1月的天数是30”,如果用户输入1等 while True: month_dict = { "1": "January", "2": "February", "3": "March", "4": "April", "5": "M
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)
给你。只需要一点重新排序和修复。基础相当坚实
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.
给你。只需要一点重新排序和修复。基础相当坚实
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.
将month\u dict
的键声明为int
主要问题是month\u dict
中的键是字符串。如果将其更改为ints:
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\u dict[month]
即可获得与其编号关联的月份名称
为循环删除
在函数中丢失
的,将使其打印单个最终结果
从中删除月份的声明和日期(月)
的编号,而
最后,也要考虑这样做,以避免重复声明相同的值。
以下是完整的代码:
month\u dict={
1:“一月”,
2:“二月”,
3:“三月”,
4:“四月”,
5:“五月”,
6:“六月”,
7:“7月”,
8:“八月”,
9:“9月”,
10:“10月”,
11:“11月”,
12:“12月”
}
def编号日期(月):
如果[1,3,5,7,8,10,12]中的月份:
打印(“月内天数”,月[月],“为31!!!”)
[4,6,9,11]中的elif月:
打印(“月数,月数,是30!!!”)
elif月==2:
打印(“月内天数”,月[月],“是28!!!”)
其他:
打印(“没有这样的月份……请检查您的文字!”)
尽管如此:
月份=整数(输入(“输入月份:”)
日期的编号(月)
我想就是这样
如果要检查闰年,请参见。将月份的键声明为int
主要问题是month\u dict
中的键是字符串。如果将其更改为ints:
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\u dict[month]
即可获得与其编号关联的月份名称
为
循环删除
在函数中丢失
的,将使其打印单个最终结果
从中删除月份的声明和日期(月)
的编号,而
最后,也要考虑这样做,以避免重复声明相同的值。
以下是完整的代码:
month\u dict={
1:“一月”,
2:“二月”,
3:“三月”,
4:“四月”,
5:“五月”,
6:“六月”,
7:“7月”,
8:“八月”,
9:“9月”,
10:“10月”,
11:“11月”,
12:“12月”
}
def编号日期(月):
如果[1,3,5,7,8,10,12]中的月份:
打印(“月内天数”,月[月],“为31!!!”)
[4,6,9,11]中的elif月:
打印(“月数,月数,是30!!!”)
elif月==2:
打印(“月内天数”,月[月],“是28!!!”)
其他:
打印(“没有这样的月份……请检查您的文字!”)
尽管如此:
月份=整数(输入(“输入月份:”)
日期的编号(月)
我想就是这样
如果您想查看闰年,请参阅。Spot on。一个增强将是考虑<代码>跳跃>代码>年…当然,这不是PO的要求。然而,这是正确的。一个增强将是考虑<代码>跳跃>代码>年…当然,这不是PO的要求。然而