Using alternative Python statements in the example

Different Python statements can be substituted for the ones used in the previous example. Experimenting in this way can help you to get used to using different approaches when structuring your Python code.

For example, the for loops could be swapped for while loops.

In this section

Original code with for loops

Replacement code with while loops

See also

How different button types affect code execution

Original code with for loops

colors = ['red','green', 'blue','pink','orange','gray','purple','magenta']

if inputs[0].changed_get():
    for i in range(len(outputs)):
        outputs[i].string_set(colors[i])
else:
    for i in range(len(outputs)):
        outputs[i].string_set(" ")

Replacement code with while loops

colors = ['red','green', 'blue','pink','orange','gray','purple','magenta']

if inputs[0].changed_get():
    i = 0    
    while i < len(outputs):
        outputs[i].string_set(colors[i])
        i = i + 1
else:
    i= 0
    while i < len(outputs):
        outputs[i].string_set(" ")		
        i = i + 1