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
200 views
in Technique[技术] by (71.8m points)

python - How do I get the asterisks to go back and forth and print start and stop?

I have tried everything I can find on this project to complete it within my professors guidelines. I cannot figure out how to simply and effectively make the asterisks go left and right and and "START!" and "STOP!" on change of direction. Any notes or help understanding is greatly appreciated. Here is what I have so far.

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 + " START!")
            indentIncreasing = False

    else:
        indent = indent - 1
        if indent == 0:
            print(stars + " STOP!")
            indentIncreasing = True

except KeyboardInterrupt:
    sys.exit()

Thank you guys!

question from:https://stackoverflow.com/questions/65926608/how-do-i-get-the-asterisks-to-go-back-and-forth-and-print-start-and-stop

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

1 Answer

0 votes
by (71.8m points)

Your function has indents missing in the second while loop to include the if statement. There is also a space missing between the '' when you multiply them. I also fixed the weird try statement. Try:

import time, sys

while True:
    try:
        print("Enter and integer between 5 and 15: ")
        userInput = int(input())
        if userInput < 5 or userInput > 15:
            continue
        break
    except ValueError:
        print("You must enter an integer.")
        

stars = ''

indent = 0
indentIncreasing = True


try:
    stars += "*" * userInput
    while True:
        print(' ' * indent, end='')  # Added a space between the first ''
        print(stars)
        time.sleep(0.1)

        # Indented the following if and else statements
        if indentIncreasing: 
            indent = indent + 1
            if indent == 20:
                print(' ' * 20 + stars + " START!") # Added a space between the ''
                indentIncreasing = False
 
        else:
            indent = indent - 1
            if indent == 0:
                print(stars + " STOP!")
                indentIncreasing = True

except KeyboardInterrupt:
    sys.exit(

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

...