Skip to content
Advertisement

How do I make it so that when I click it changes the list

I have a list full of colors; I’m trying to make it so that when I click a button, it moves over one position in a list, thus changing the color of a shirt. How would I make it so that when I click the button it shifts to the next value?

e.g.

car = ["red","blue","white","green","orange"]

def click(button):
    car[0]  += 1

(I know this is wrong, but do you know what I should do?)

When you click button, the car should turn from red to blue. click again, blue to white, etc.

Thanks!

Advertisement

Answer

As per @Barmar recommendation, here is the code:

car = ["red","blue","white","green","orange"]

index = 0
def click(button):
    global index
    index += 1
    car[index]

Keep in mind clicking after “orange” will cause an error, as there are no more colors. I would recommend adding a guard for that instance.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement