gpt4 book ai didi

python - 如何在不同乘法表之间插入换行符?

转载 作者:行者123 更新时间:2023-12-01 02:33:55 25 4
gpt4 key购买 nike

我编写此代码是为了打印从 1 到 9 的乘法表,但它打印出来时在不同的表之间没有换行。有谁知道如何解决这个问题吗?

for i in range(1, 10):
for j in range(1, 10):
k = i * j
print(i,"x",j, "=", k)

结果是这样的:

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18

最佳答案

for i in range(1, 10): 循环的迭代之间打印一个空行,这将按您要打印的乘积的数字分隔表格

for i in range(1, 4):
for j in range(1, 4):
k = i * j
print(i,"x",j, "=", k)
print()

>> 1 x 1 = 1
>> 1 x 2 = 2
>> 1 x 3 = 3

>> 2 x 1 = 2
>> 2 x 2 = 4
>> 2 x 3 = 6

>> 3 x 1 = 3
>> 3 x 2 = 6
>> 3 x 3 = 9

关于python - 如何在不同乘法表之间插入换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46469249/

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