Skip to content
Advertisement

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

Advertisement

Answer

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