python-3.x - 在 while 循环中打印额外的逗号 - Python

标签 python-3.x printing

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

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

最佳答案

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

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

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

关于python-3.x - 在 while 循环中打印额外的逗号 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58284847/

相关文章:

python - 如何在 Python 2.7 中加载 Python 3 pickle *不改变它*?

windows - 如何在 Windows 上运行 python dbus?

html - CSS、HTML - 为什么在打印我的页面时会出现页面标题标签 (<html><head>&lt;title&gt;THIS CONTENT&lt;/title&gt;...) 的内容?

c++ - opengl 在屏幕上打印文本变量 - 菜单

javascript - 使用 JSBarcode 库无法正确打印条码

javascript - 在 QZ-Print Java Applet 中调用函数在 PHP 中不起作用

java - 打印 Java 数组的最简单方法是什么?

python-3.x - Python 如何知道 π 的所有这些变体都是相同的变量名?

python-3.x - 如何获取实际数据而不是对象引用?

python - 如何使用变量而不是大括号内的数字来格式化字符串?