Python遍历字典x的次数?

时间:2020-10-14 04:00:39

标签: python

我有多种颜色。我希望程序“绘制”页面,移至下一页,重复绘制10次。“绘制”另一页面,数组中的下一个颜色。

但是,以下代码似乎存在错误。我的代码将遍历同一页面上的颜色,而无需转到/移至下一页。因此,我最终得到了几张相同颜色的页面。

<Datepicker mode="year" onPanelChange={(value) => console.log(value)} />
for x in range(10):
    for i in colors:
       turnPage()
       paint(i)

编辑:在下面进行编辑并添加完整代码,以供参考/澄清。

What the program will do is
Blue
Red
Green
turnPage()
#page ends up green
Blue
Red
Green
TurnPage()
#page ends up green

But I want the output to be
Blue
turnPage()
Red
turnPage()
Green 
turnPage()

1 个答案:

答案 0 :(得分:2)

您需要为此使用generators

def turnpage():
    print("turned")

def generator(colors):
    while True:
        for color in colors:
            yield color

colors = ["green","red","blue"]

gen = generator(colors)

for page_number in range(10):
    print(next(gen))
    turnpage()

输出:

green 
turned
red
turned
blue
turned
green 
turned
red
turned
blue
turned
green 
turned
red
turned
blue
turned
green 
turned