【问题标题】:Is this correct formatting这是正确的格式吗
【发布时间】:2019-05-21 02:36:07
【问题描述】:

我正在努力确保我的代码是正确的

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

>>> 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 个月是一月 第 2 个月是二月……。 ……第 12 个月是 12 月 可选第 1 个月是 1 月,新年快乐

【问题讨论】:

  • 不确定您希望我们如何知道您未描述的问题的正确性。
  • 这就是它的样子:
  • 创建一个包含一年中月份的列表(或只有元组,没有字典)。 (不要对数字进行硬编码)创建一个循环以从列表中打印数字和一年中的月份。输出应该是这样的: 第 1 个月是一月 第 2 个月是二月……。 ……第 12 个月是 12 月 可选第 1 个月是 1 月,新年快乐

标签: python loops tuples


【解决方案1】:

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

>>> 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',)]
>>> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多