我正在学习Python中的递归,我写了一个小程序,为了博学的目的,我想改进它。在重复程序中实现递归
该程序重复打印改变颜色的星号行。该程序运行,直到我停止它。现在它按预期工作,但通过查看它,我可以告诉我们必须有更好的方式来递归地编写它,或者使用另一种方法。
请张贴一些答案,演示如何改善此计划。如果你不想要,你不必使用termcolor
模块。
下面是我的代码:
import random
from termcolor import colored
s = random.choice('*******************',)
colors = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
text_color = ""
def set_color(colors):
global text_color
text_color = random.choice(colors)
while True:
for a in s:
for b in s:
for c in s:
for d in s:
for e in s:
for f in s:
for g in s:
for h in s:
for i in s:
for j in s:
for k in s:
for l in s:
for m in s:
for n in s:
for o in s:
for p in s:
print(colored(a, text_color)), (colored(b, text_color)), (colored(c, text_color)), (colored(d, text_color)), (colored(e, text_color)), (colored(f, text_color)), (colored(g, text_color)), (colored(h, text_color)), (colored(i, text_color)), (colored(j, text_color)), (colored(k, text_color)), (colored(l, text_color)), (colored(m, text_color)), (colored(n, text_color)), (colored(o, text_color)), (colored(p, text_color)), (colored(a, text_color)), (colored(b, text_color)), (colored(c, text_color)), (colored(d, text_color)), (colored(e, text_color)), (colored(f, text_color)), (colored(g, text_color)), (colored(h, text_color)), (colored(i, text_color)), (colored(j, text_color)), (colored(k, text_color)), (colored(l, text_color)), (colored(m, text_color)), (colored(n, text_color)), (colored(o, text_color)), (colored(p, text_color)); set_color(colors)
真实的阶梯....这更适合[codereview.se] – MYGz
将您的建议带到答案框! – celestialroad
'random.choice('*******************',)'应该实现的是什么? –