我正在尝试用Python编写一段代码来生成斐波那契数列,然后对每个数字进行颜色编码。
到目前为止我已经:
import time
a = 1
b = 1
while True:
al = str(list(str(a)))
bl = str(list(str(b)))
print(al)
time.sleep(0.25)
print(bl)
time.sleep(0.25)
a = a+b
b = a+b
但我不知道如何对数字进行颜色编码,更不用说如何渲染任何形状了。
打印彩色输出的方法之一是使用 colorama 模块。您可以在这里找到文档https://pypi.org/project/colorama/
from colorama import Fore, Back, Style
print(Fore.RED + 'text in red color')
print(Back.GREEN + 'text with green background')
print(Style.RESET_ALL) #to reset whenever needed
例如,这将以替代颜色打印每个字符
import time
import colorama
def print_list_in_color(al):
count = 0
for i in al:
count +=1
if count%2 == 0 :
print (colorama.Fore.RED + i,end='')
else :
print (colorama.Fore.GREEN + i,end='')
print (colorama.Style.RESET_ALL,end='')
print()
a = 1
b = 1
while True:
al = str(list(str(a)))
bl = str(list(str(b)))
print_list_in_color(al)
time.sleep(0.25)
print_list_in_color(bl)
time.sleep(0.25)
a = a+b
b = a+b
简单地说,这将以替代颜色打印每个列表
import time
import colorama
a = 1
b = 1
while True:
al = str(list(str(a)))
bl = str(list(str(b)))
print(colorama.Fore.RED+al)
time.sleep(0.25)
print(colorama.Fore.GREEN+bl)
time.sleep(0.25)
a = a+b
b = a+b