Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

python - 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, 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()
question from:https://stackoverflow.com/questions/65929177/how-can-i-get-start-to-print-after-the-first-line-of-asterisks-when-i-run-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about just adding a print statement before the while loop and initializing indent at 1 instead of 0?

indent = 1
indentIncreasing = True

try:
    stars += "*" * userInput
    print(stars + ' START!')
    while True:
        print(' ' * indent, end='')
        print(stars)
        time.sleep(0.1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...