我想打印以下模式,但长度的 while 循环没有运行。当我运行代码时,它只打印一个三角形 在代码下方有预期输出和实际输出:
height = int(input("Enter height ? "))
length = int(input("Enter length ? "))
spaces = height-1
spaces2 = 0
while length > 0:
for n in range(height):
for i in range(spaces):
print(' ',end="")
print('/',end="")
for j in range(spaces2):
print(' ',end="")
print('\\',end="")
for k in range(spaces):
print(' ', end="")
print('')
height-=1
spaces-=1
spaces2+=2
length-=1
预期输出:
高度 = 5 长度 = 3
/\ /\ /\
/ \ / \ / \
/ \ / \ / \
/ \ / \ / \
/ \/ \/ \
代码运行时的输出:
高度 = 5 长度 = 3
/\
/ \
/ \
/ \
/ \
该while
循环工作。但是,在第一时间通过while
循环代码改变的值height
,spaces2
和spaces
。第二次while
循环时,这些值都为零,因此for
循环什么也不做。为了表明这一点,请在 lineprint(length)
正下方添加一行length -= 1
。您将看到while
报告的循环的3 次迭代。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句