简体   繁体   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?

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.我使用了一个变量来保存STARTSTOP消息,然后我可以将其他行留空。 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.

相关问题 如何使用csv使Python读取一行中的第一个单词而不是第一个字符? - How can I make Python using csv read the first word in a line rather than the first character? 如何来回获取 go 的星号并打印开始和停止? - How do I get the asterisks to go back and forth and print start and stop? 如何立即运行生成器函数的初始化代码,而不是在第一次调用时? - How can I run the initialization code for a generator function immediately, rather than at the first call? 如何打印值而不是张量? - How can I print a value rather than a tensor? 如何在旋转框中按行索引,而不是按字符号? - How can I index by line in a spinbox, rather than by character number? 我如何只打印最终列表结果而不是以前的列表? - How do i just print the final list result rather than the previous lists too? 如何使用 Python 在命令行中创建交互式应用程序,而不仅仅是返回 output 的脚本? - How do I make an interactive application at the command line with Python, rather than just a script which returns output? 我怎样才能让这段代码不仅仅包含循环中的第一次出现? - How can I get this code to include more than just the first occurrence in the loop? 如何仅一行打印9x9表格? - How can I print a 9x9 table in just one line? 如何在一行中而不是单独打印列表的值 - How do I print the values of a list in one line rather than separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM