尝试在Python中格式化乘法表

Gnl*_*ken 1 python multiplication

好的,所以我正在为我的Python课程做作业.教授希望我使用%格式说明符制作乘法表.

x = 0
y = 0

for y in range(1, 11):
    for z in range(1, 11):
        print("%10i" %(y*z))
Run Code Online (Sandbox Code Playgroud)

我知道格式说明符是错误的,但是如何使用我的代码中的格式类型使其看起来像一个乘法表?

我希望它看起来像:

1 2 3  4  5  6  7  8  9 10
2 4 6  8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
. . .
10 20 30 40 50 60 70 80 90 100
Run Code Online (Sandbox Code Playgroud)

谢谢大家.

Bar*_*mar 5

for y in range(1, 11):
    for z in range(1, 11):
        print(" %3i" %(y*z), end="")
    print("")
Run Code Online (Sandbox Code Playgroud)

您不需要初始化变量,因为for这样做.

%3i对于此表中的所有值都足够宽; %10i会使桌子非常宽.

end=""在打印每个数字后保持它不添加换行符,因此相同值的所有数字y将打印在同一行上.然后print("")在行的末尾添加换行符.

DEMO