这是正确的格式吗

时间:2019-05-21 02:36:08

标签: python loops tuples

我正在尝试确保我的代码正确

我不确定这是否是正确的格式

>>> month_name= [('Month 1 is January'),('Month 2 is February'),('Month 3 is March'),('Month 4 is April'),('Month 5 is May'),('Month 6 is June'),('Month 7 is July'),('Month 8 is August'),('Month 9 is September'),('Month 10 is October'),('Month 11 is November'),('Month 12 is December')]
>>> print(month_name)
['Month 1 is January', 'Month 2 is February', 'Month 3 is March', 'Month 4 is April', 'Month 5 is May', 'Month 6 is June', 'Month 7 is July', 'Month 8 is August', 'Month 9 is September', 'Month 10 is October', 'Month 11 is November', 'Month 12 is December']

它必须像这样出来: 创建一个包含一年中各个月份的列表(或仅包含元组,不包含字典)。 (不要对数字进行硬编码)创建循环以从列表中打印数字和一年中的月份。输出应如下所示:第1个月是1月第2个月是2月…。 …。第12月是12月(可选)第1月是1月,新年快乐

1 个答案:

答案 0 :(得分:0)

在表达式周围的一对括号不会构成一个元组。如果要创建一个元素元组,则需要在括号中加一个逗号:

>>> month_name= [('Month 1 is January',),('Month 2 is February',),('Month 3 is March',),('Month 4 is April',),('Month 5 is May',),('Month 6 is June',),('Month 7 is July',),('Month 8 is August',),('Month 9 is September',),('Month 10 is October',),('Month 11 is November',),('Month 12 is December',)]
>>> month_name
[('Month 1 is January',), ('Month 2 is February',), ('Month 3 is March',), ('Month 4 is April',), ('Month 5 is May',), ('Month 6 is June',), ('Month 7 is July',), ('Month 8 is August',), ('Month 9 is September',), ('Month 10 is October',), ('Month 11 is November',), ('Month 12 is December',)]
>>>