【发布时间】:2021-07-29 08:43:42
【问题描述】:
我很难弄清楚为什么我的代码不能按预期工作。 我的输出看起来像一堆星号,而不是正确的之字形。
import time, sys
indent = 0 # How many spaces to indent.
indentIncreasing = True #Whether or not the indent increases
try:
while True:
#The main program loop
print(' ' * indent, end='')
print('********')
time.sleep(0.1) # Pause for 1/10 of a second.
if indentIncreasing:
#Increase the number of spaces:
indent = indent + 1
if indent == 20:
indentIncreasing = False
# Change direction:
else:
#decrease the number of spaces:
indent = indent - 1
if indent == 0:
#Change direction:
indentIncreasing = True
except KeyboardInterrupt:
sys.exit()
我的输出:
期望的输出:
【问题讨论】:
-
您能否包括所需的行为和当前输出?
-
是的,我在一些链接中发布了它,希望它足够好,因为这是我的第一篇文章。 :D
-
说到缩进,我认为你需要缩进从
if indentIncreasing:开始到else的代码块。 -
现在,
if语句在 while 循环之外,所以indent永远不会改变。