小孔爱学习 发表于 2021-10-22 17:14:32

python 用while语句编写99乘法表为什么不能呈现阶梯状

本帖最后由 小孔爱学习 于 2021-10-22 17:15 编辑

i = int(1)
while i <= 9:
    j = 1
    while j <= i:
      print(i,'X',j,'=',i*j,' ')
      j = j+1
    i = i+1

binzai_007 发表于 2021-10-22 17:46:43

i = int(1)
while i <= 9:
    j = 1
    while j <= i:
      print(i,'X',j,'=',i*j, end=' ')
      j = j+1
    print()
    i = i+1

这样既可了,默认情况下,print结束后自动换行,如果你不需要换行,需要手工填写end对应的标志
然后需要换行的时候,直接print()既可

傻眼貓咪 发表于 2021-10-22 17:52:22

for i in range(1, 10):
    for j in range(1, i+1):
      print(f"{i} × {j} = {i*j}", end = ' ')
    print()1 × 1 = 1
2 × 1 = 2 2 × 2 = 4
3 × 1 = 3 3 × 2 = 6 3 × 3 = 9
4 × 1 = 4 4 × 2 = 8 4 × 3 = 12 4 × 4 = 16
5 × 1 = 5 5 × 2 = 10 5 × 3 = 15 5 × 4 = 20 5 × 5 = 25
6 × 1 = 6 6 × 2 = 12 6 × 3 = 18 6 × 4 = 24 6 × 5 = 30 6 × 6 = 36
7 × 1 = 7 7 × 2 = 14 7 × 3 = 21 7 × 4 = 28 7 × 5 = 35 7 × 6 = 42 7 × 7 = 49
8 × 1 = 8 8 × 2 = 16 8 × 3 = 24 8 × 4 = 32 8 × 5 = 40 8 × 6 = 48 8 × 7 = 56 8 × 8 = 64
9 × 1 = 9 9 × 2 = 18 9 × 3 = 27 9 × 4 = 36 9 × 5 = 45 9 × 6 = 54 9 × 7 = 63 9 × 8 = 72 9 × 9 = 81

白two 发表于 2021-10-22 22:50:43

每个内循环需要加上 print 的 end 参数为 ''
不然会默认换行
页: [1]
查看完整版本: python 用while语句编写99乘法表为什么不能呈现阶梯状