Python 如何让星号来回移动并打印开始和停止?
我已经尝试了我能找到的关于这个项目的一切,以在我教授的指导下完成它。我不知道如何简单有效地使星号左右移动,以及在转向时“开始”和“停止”。非常感谢任何注释或帮助理解。这是我到目前为止所拥有的Python 如何让星号来回移动并打印开始和停止?,python,Python,我已经尝试了我能找到的关于这个项目的一切,以在我教授的指导下完成它。我不知道如何简单有效地使星号左右移动,以及在转向时“开始”和“停止”。非常感谢任何注释或帮助理解。这是我到目前为止所拥有的 import time, sys while True: try: print("Enter and integer between 5 and 15: ") userInput = int(input()) if userInp
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()
导入时间,系统
尽管如此:
尝试:
打印(“输入介于5和15之间的整数:”)
userInput=int(input())
如果userInput<5或userInput>15:
持续
其他:
打破
除值错误外:
打印(“您必须输入一个整数。”)
持续
星星=“”
缩进=0
递增=真
尝试:
stars+=“*”*用户输入
尽管如此:
打印(“”*缩进,结束=“”)
印刷品(星号)
睡眠时间(0.1)
如果增加:
缩进=缩进+1
如果缩进=20:
打印(''*20+星星+“开始!”)
缩进=假
其他:
缩进=缩进-1
如果缩进=0:
打印(星号+停止!)
递增=真
除键盘中断外:
sys.exit()
谢谢你们 函数在第二个while循环中缺少缩进以包含if语句。将“”相乘时,它们之间也缺少一个空格。 我还修复了奇怪的try语句。 尝试: 导入时间,系统 尽管如此: 尝试: 打印(“输入介于5和15之间的整数:”) userInput=int(input()) 如果userInput<5或userInput>15: 持续 打破 除值错误外: 打印(“您必须输入一个整数。”) 星星=“” 缩进=0 递增=真 尝试: stars+=“*”*用户输入 尽管如此: 打印(''*缩进,结束='')#在第一个'' 印刷品(星号) 睡眠时间(0.1) #缩进以下if和else语句 如果增加: 缩进=缩进+1 如果缩进=20: 打印('*20+星星+“开始!”)#在'' 缩进=假 其他: 缩进=缩进-1 如果缩进=0: 打印(星号+停止!) 递增=真 除键盘中断外: 系统出口(
这是可行的。缩进和索引增量语句的放置位置存在问题。我对其进行了编辑,使其显示“开始”和“停止”在正确的位置。最重要的是,您需要一个空格“”,而不是一个空字符串“” 导入时间,系统 尽管如此: 尝试: userInput=int(输入(“输入5到15之间的整数:”) 如果userInput<5或userInput>15: 持续 其他: 打破 除值错误外: 打印(“您必须输入一个整数。”) 持续 星星=“” 缩进=0 递增=真 尝试: stars+=“*”*用户输入 尽管如此: 缩进+=1 打印(“”*缩进,结束=“”) 印刷品(星号) 睡眠时间(0.1) 如果增加: 如果缩进=20: 打印(''*20+星星+“停止!”) 缩进=假 其他: 缩进-=2 如果缩进=0: 打印(星号+开始!) 递增=真 除键盘中断外: sys.exit()
嗨,很有趣,你想做动画吗?
尝试使用否则
无效。你正在while
循环中使用它。非常感谢你的帮助!我该如何让输出说从第一行星号开始,而不仅仅是后面的方向改变?
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()