【问题标题】:Printing extra comma in while loop - Python在while循环中打印额外的逗号 - Python
【发布时间】:2019-10-08 10:46:28
【问题描述】:

我不想打印循环中的最后一个逗号。

countWhile = 5
while countWhile > 0:
    if countWhile == 0:
        break
    else:
        print(countWhile, end=', ')
    countWhile -= 1
print("While Loop Finished.")

实际结果:5、4、3、2、1,

预期结果:5、4、3、2、1

【问题讨论】:

    标签: python-3.x printing


    【解决方案1】:

    这是一种可能的解决方案:

    countWhile = 5
    myList = []
    while countWhile > 0:
        if countWhile == 0:
            break
        else:
            myList.append(str(countWhile))
        countWhile -= 1
    print(', '.join(myList))
    print("While Loop Finished.")
    

    如果你愿意,我可以尝试寻找另一个更简单的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      相关资源
      最近更新 更多