我有一个嵌套的for循环:
for i in range(0,6):
for j in range(0,10):
print(j, end="")
print("\n",0)
结果如下:
0123456789
0
0123456789
0
0123456789
0
0123456789
0
0123456789
0
0123456789
0
如何从结果中省略最后一个0
?
答案 0 :(得分:2)
您可以保留一个变量以查看该过程是否完整且不需要更多的零。
done = False
for i in range(0,6):
for j in range(0,10):
print(j, end="")
if i == 5 and j == 9:
done = True
if not done:
print("\n",0)
# You can also use this variable to break the loop
# after the process is complete.
答案 1 :(得分:2)
您只需按以下方式添加if:
for i in range(0, 6):
for j in range(0, 10):
print(j, end="")
if i != 5:
print("\n", 0)