gpt4 book ai didi

python - 如何使用 python 中的 while 循环语句每行打印 6 个值

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:50 25 4
gpt4 key购买 nike

这是我的代码。我需要一些帮助来弄清楚打印功能。

x=0

while x < 999:
if x%40 ==0:
print(format(x,'7d'),end='')
x = x + 1

我尝试使用的打印功能对我不起作用。我的代码也不会按照我想要的方式打印出值。我希望它每行打印出 6 个值。

我希望它像这样打印。

40 80 120 160 200 240

280 320 360 400 440 480

但是它直接打印出来,就像请帮忙一样。

最佳答案

在 python 中,总是有一种简单的方法来做某事:

arr = [format(x, '7d') for x in range(40, 999, 40)]
print('\n'.join(''.join(arr[i:i+6]) for i in range(0, len(arr), 6)))

哪个输出:

 40     80    120    160    200    240
280 320 360 400 440 480
520 560 600 640 680 720
760 800 840 880 920 960

关于python - 如何使用 python 中的 while 循环语句每行打印 6 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46040569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com