在python中如何使用while?
我应该只使用在python中如何使用while?,python,Python,我应该只使用while和print来完成作业。我尝试了一种不同的方法来处理这个问题,但还是被卡住了 预期产出: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1 我得到的是: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 这是我的密码: j = 1 i = 1 t = 6 x = 10 d = 1 wh
while
和print
来完成作业。我尝试了一种不同的方法来处理这个问题,但还是被卡住了
预期产出:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
我得到的是:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
这是我的密码:
j = 1
i = 1
t = 6
x = 10
d = 1
while i <= 6:
n = 1
space = -3
while space <= j:
print(" " * x, end="")
space += 1
break
while n <= i:
print('%d '%n, end="")
n += 1
print("")
i += 1
x -= 2
j=1
i=1
t=6
x=10
d=1
而我你就快到了。倒数;i、 e.更改以下行
n = 1
到
及
还可以尝试一行解决方案,以获得乐趣:
>>> print("\n".join([" " * (7 - i) * 2 + " ".join([str(x) for x in reversed(range(1, i))]) for i in range(2, 8)]))
你就快到了。倒数;i、 e.更改以下行
n = 1
到
及
还可以尝试一行解决方案,以获得乐趣:
>>> print("\n".join([" " * (7 - i) * 2 + " ".join([str(x) for x in reversed(range(1, i))]) for i in range(2, 8)]))
您必须按与当前相反的顺序打印:
n =6
i = 1
tCol = n*2 -1
while i <=n:
cCount = i*2
spaceCount = tCol - cCount +1
s=1
while s<=spaceCount:
print(" ",end="")
s+=1
t =i
while t>=1:
print(t, end="")
if(t!=1):
print(" ", end="")
t-=1
print()
i+=1
您可以更改n的值以获得任意数字您必须按与当前数字相反的顺序打印:
n =6
i = 1
tCol = n*2 -1
while i <=n:
cCount = i*2
spaceCount = tCol - cCount +1
s=1
while s<=spaceCount:
print(" ",end="")
s+=1
t =i
while t>=1:
print(t, end="")
if(t!=1):
print(" ", end="")
t-=1
print()
i+=1
您可以将n的值更改为任意数字谢谢您的注意,我没有注意到空格,现在我已经编辑了我的代码谢谢您的注意,我没有注意到空格,现在我已经编辑了我的代码
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
n =6
i = 1
tCol = n*2 -1
while i <=n:
cCount = i*2
spaceCount = tCol - cCount +1
s=1
while s<=spaceCount:
print(" ",end="")
s+=1
t =i
while t>=1:
print(t, end="")
if(t!=1):
print(" ", end="")
t-=1
print()
i+=1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1