首页 > 解决方案 > Python 全局计数

问题描述

我在 CS 的第一年,我正在尝试存储一个不断计数 1-6 的变量。我希望它在颜色 1-6 之间循环。

我希望它看起来像这样:图片

我当前的代码只是操纵一个字符串,让它看起来像是在滚动。

from time import sleep
from replit import clear
from termcolor import colored

string = input("Input a string to manipulate: ")
clear()

colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'magenta']


# THIS PART DOESNT WORK!!!:
# while True:
#   global y
#   for y in range(6):
#    y += 1

for x in range(23):
  print(colored(string, colors[y]))
  sleep(0.1)

for x in range(19):
  space = " " * x
  print(space+string)
  sleep(0.1)

for x in range(18,0,-1):
  print(" "*x+string)
  sleep(0.1)

print(string)

在我输入字符串后,它只是停止并且不做任何其他事情。如果有人能够提供帮助,将不胜感激!谢谢

标签: pythonpython-3.x

解决方案


from time import sleep
from replit import clear
from termcolor import colored

string = input("Input a string to manipulate: ")
clear()

colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'magenta']

while True:
  for x in range(6):
    print(colored(string, colors[x]))
    sleep(0.1)

使用它并从那里构建。


推荐阅读