[英]Using tuples and input functions
My task (for class): Write a program that takes a month number (like 3) and outputs the month name (like March).我的任务(类):编写一个程序,它需要一个月份数(如 3)并输出月份名称(如 March)。 Use a list or tuple to store all of the month names and then use the month number to index.使用列表或元组存储所有月份名称,然后使用月份编号进行索引。 Example:例子:
>>> Enter a month number from 1 to 12: 3
>>> That is March.
This is what I've tried:这是我尝试过的:
from collections import namedtuple
month = int(input('Any month number between 1-4: '))
month_name = namedtuple('Month',['January','February','March','April',])
month_number = month_name(1,2,3,4)
You can use this code for the purpose:您可以将此代码用于以下目的:
month_number = (int(input("Enter month Number : ")))-1
month_name = ('January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
)
for i in range(len(month_name)):
if month_number==i:
print(month_name[i])
Try this:尝试这个:
monthes=['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
while True: #repeat asking for the number
index=input('Enter a month number from 1 to 12: ')
print(montheis[int(index)-1])
i used 'monthes[int(index) -1 ]' be cause in the indext of al list or eny itmes type 0
means the first item and 1
the second one, and so on, and -1
the last item and -2-
the item be fore last我使用 'monthes[int(index) -1 ]' 是因为在 al 列表或 eny 项目的索引中,类型0
表示第一项, 1
表示第二项,依此类推, -1
最后一项, -2-
项目放在最后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.