[英]How can I get “START!” to print after the first line of asterisks when I run it, rather than just the changes of direction?
It will print start and stop, but not on the first line?它会打印开始和停止,但不是在第一行? How can I get "START," to print after the first line of asterisks when I run it?当我运行它时,如何在第一行星号之后打印“START”? rather than just the changes of direction?而不仅仅是方向的变化?
import time, sys
while True:
try:
print("Enter and integer between 5 and 15: ")
userInput = int(input())
if userInput < 5 or userInput > 15:
continue
else:
break
except ValueError:
print("You must enter an integer.")
continue
stars = ''
indent = 0
indentIncreasing = True
try:
stars += "*" * userInput
while True:
print(' ' * indent, end='')
print(stars)
time.sleep(0.1)
if indentIncreasing:
indent = indent + 1
if indent == 20:
print(' ' * 20 + stars + " STOP!")
indentIncreasing = False
else:
indent = indent - 1
if indent == 0:
print(stars + " START!")
indentIncreasing = True
except KeyboardInterrupt:
sys.exit()
How about just adding a print statement before the while
loop and initializing indent
at 1 instead of 0?在while
循环之前添加一个打印语句并将indent
初始化为 1 而不是 0 怎么样?
indent = 1
indentIncreasing = True
try:
stars += "*" * userInput
print(stars + ' START!')
while True:
print(' ' * indent, end='')
print(stars)
time.sleep(0.1)
Check for the start/stop conditions first.首先检查启动/停止条件。
I've used a variable to hold the START
and STOP
messages, then I can leave it blank for the other lines.我使用了一个变量来保存START
和STOP
消息,然后我可以将其他行留空。 This allows a single print()
line for all cases.这允许在所有情况下使用单个print()
行。
stars = "*" * userInput
indent = -1
indentIncreasing = True
try:
while True:
startstop = ''
if indentIncreasing:
indent = indent + 1
if indent == 20:
startstop = ' STOP!'
indentIncreasing = False
else:
indent = indent - 1
if indent == 0:
startstop = ' START!'
indentIncreasing = True
print(' ' * indent + stars + startstop)
time.sleep(0.1)
except KeyboardInterrupt:
sys.exit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.