繁体   English   中英

如何来回获取 go 的星号并打印开始和停止?

[英]How do I get the asterisks to go back and forth and print start and stop?

我已经尝试了我在这个项目上能找到的一切,以在我的教授指导下完成它。 我不知道如何简单有效地使星号 go 左右以及“开始”。 和“停止”。 关于方向的改变。 非常感谢任何注释或帮助理解。 这是我到目前为止所拥有的。

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

感谢你们!

您的 function 在第二个 while 循环中缺少缩进以包含 if 语句。 当您将它们相乘时,'' 之间也缺少一个空格。 我还修复了奇怪的 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(

这行得通。您的缩进和放置索引增量语句的位置存在问题。 我对其进行了编辑,所以它说在正确的地方开始和停止。 最重要的是,您需要一个空格 ' ' 而不是空字符串 ''。

import time, sys

while True:
    try:
        userInput = int(input("Enter and integer between 5 and 15: "))
        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:
        indent += 1
        print(' ' * indent, end='')
        print(stars)
        time.sleep(0.1)

        if indentIncreasing:
            if indent == 20:
                print(' ' * 20 + stars + " STOP!")
                indentIncreasing = False
        else:
            indent -= 2
            if indent == 0:
                print(stars + " START!")
                indentIncreasing = True

except KeyboardInterrupt:
    sys.exit()
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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