Python中的For/While循环三角形不起作用
处理我的第二个三角形,但它没有给我使用while嵌套循环所需的结果,我应该有以下输出 使用for循环:Python中的For/While循环三角形不起作用,python,for-loop,while-loop,nested-loops,Python,For Loop,While Loop,Nested Loops,处理我的第二个三角形,但它没有给我使用while嵌套循环所需的结果,我应该有以下输出 使用for循环: 0 01 012 0123 01234 012345 使用while循环: 5 45 345 2345 12345 012345 代码: 使用while循环 012345 01234 0123 012 01 0 你的逻辑是错误的。还有很多语法错误。使用以下命令: print('Using While loop') print() cnt = 6 v = cn
0
01
012
0123
01234
012345
使用while循环:
5
45
345
2345
12345
012345
代码:
使用while循环
012345
01234
0123
012
01
0
你的逻辑是错误的。还有很多语法错误。使用以下命令:
print('Using While loop')
print()
cnt = 6
v = cnt
while(cnt != -1):
print(' ' * cnt, end='')
for j in range(cnt, v):
if j < cnt+1:
print(j, end='')
else:
print(j, end='')
cnt -=1
print()
你有很多语法错误使用这个
print('Using for loop')
print()
M = 6 #constant
cnt = 1
for i in range(0,M):
for j in range(0,cnt):
if(j<M):
print(j,'',end='')
cnt+=1
print()
print('\nUsing While loop\n')
cnt = 0
while(cnt != M):
for j in range(0,M-(cnt+1)):
print(' ','',end='')
for j in range(0,cnt+1):
print(M-(cnt+1-j),'',end='')
cnt +=1
print()
print()
print('Using for loop')
打印()
M=6#常数
cnt=1
对于范围(0,M)内的i:
对于范围(0,cnt)内的j:
如果(j这里有一个while循环解决方案,没有任何for循环:
print('Using While loop')
print()
cnt = 6
v = cnt
while(cnt != -1):
j = cnt
print(' ' * cnt, end='')
while(j < v):
print(j, end='')
j += 1
cnt -=1
print()
print('使用While循环')
打印()
cnt=6
v=碳纳米管
而(cnt!=-1):
j=碳纳米管
打印(''*cnt,结束='')
而(j
你的while循环有很多语法错误。我不确定给你的问题的限制,但是你的while循环代码不应该只使用while循环而不是for循环吗?现在请参阅语法错误。Thx@skaul05.Yes@gdef_u我只使用while循环..不使用for循环。:-)Thx供你反馈。这里是新手,它被证明是非常有趣的。太好了,我为第二部分添加了一个只有while循环的解决方案。感谢您的反馈。感谢您的时间…将比较我的工作,了解我哪里出了问题。同时,感谢你的时间。
Using While loop
5
45
345
2345
12345
012345
print('Using for loop')
print()
M = 6 #constant
cnt = 1
for i in range(0,M):
for j in range(0,cnt):
if(j<M):
print(j,'',end='')
cnt+=1
print()
print('\nUsing While loop\n')
cnt = 0
while(cnt != M):
for j in range(0,M-(cnt+1)):
print(' ','',end='')
for j in range(0,cnt+1):
print(M-(cnt+1-j),'',end='')
cnt +=1
print()
print()
print('Using While loop')
print()
cnt = 6
v = cnt
while(cnt != -1):
j = cnt
print(' ' * cnt, end='')
while(j < v):
print(j, end='')
j += 1
cnt -=1
print()