2017-02-10 7 views
0

私は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) 
+4

真実への階段...これは[codereview.se]に適しています – MYGz

+0

回答箱にあなたの意見をお寄せください! – celestialroad

+0

'random.choice( '*******************'、)'とは何ですか? –

答えて

3

のでrandom.choice('*******************',)文字列からランダムな文字を選択します。すべての文字が '*'なので、基本的に式全体をs = '*'で置き換えることができます。

ネストされたループの形式はfor x in sです。 s = '*'以降、これはfor x in '*'に相当します。 1文字の文字列の場合、ループは1回の繰り返ししかないため、x = 's'に置き換えることができます。

今注目しなければならないのは、aからpまでのすべての変数が等しく、アスタリスクを含んでいることです。それらはすべて削除することができ、代わりにsを使用します。

最後に、巨大なprintステートメントはcolored(s, text_color)でいっぱいです。

import random 
from termcolor import colored 

s = '*' 
colors = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] 
text_color = '' 

def set_color(colors): 
    global text_color 
    text_color = random.choice(colors) 

text_color = set_color(colors) 
while True: 
    print(' '.join(colored(s, text_color) for _ in range(32))) 
    set_color(colors) 

それをさらに簡素化することができる::彼らはあなたが以下のプログラムで終わる' '.join(colored(s, text_color) for _ in range(32))

に置き換えることができ

import random 
from termcolor import colored, COLORS 
while True: 
    color = random.choice(COLORS.keys()) 
    print(' '.join(colored('*', color) for _ in range(32))) 

これはもっと楽しいです、それは両方の背景を使用します。前景色、空白を使用しないでください。

import os 
from random import choice 
from termcolor import colored, COLORS, HIGHLIGHTS 
r, c = os.popen('stty size', 'r').read().split() 
WIDTH=int(r+c) # actual terminal width under Linux 
while True: 
    print ''.join(colored('▄', choice(COLORS.keys()), choice(HIGHLIGHTS.keys())) for _ in range(WIDTH)), 

出力: random colors everywhere!

+0

優秀、これは本当に洞察力があります。 – celestialroad

+0

ニース、あなたはターミナルサイズを持っています:) –

関連する問題