繁体   English   中英

当我运行它时,如何在第一行星号之后打印“START”? 而不仅仅是方向的变化?

[英]How can I get “START!” to print after the first line of asterisks when I run it, rather than just the changes of direction?

它会打印开始和停止,但不是在第一行? 当我运行它时,如何在第一行星号之后打印“START”? 而不仅仅是方向的变化?

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()

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)

首先检查启动/停止条件。

我使用了一个变量来保存STARTSTOP消息,然后我可以将其他行留空。 这允许在所有情况下使用单个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM