python - 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)
在我输入字符串后,它只是停止并且不做任何其他事情。如果有人能够提供帮助,将不胜感激!谢谢
解决方案
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)
使用它并从那里构建。
推荐阅读
- sql - AWS Kinesis Tumbling-Sliding Window Count(流行/趋势计数)
- javascript - 取消绑定后jquery函数停止
- c# - Windows 窗体未在 Listbox_SelectedIndexChanged 中实现异常
- python - python比较CSV并找出差异
- objective-c - WKWebView - 放大后内容不合适
- php - codeigniter 中的“无法找到 session.php”消息
- wpf - WPF TreeView KeyBinding Ctrl + Down
- http - 为(可能)非常大的文本文件发送部分更新/添加
- r - 将列表中的表达式捕获为文本
- vb.net - 如何在 Visual Basic 中更改 Datagridview 的设计?