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.
JavaScript
x
5
1
car = ["red","blue","white","green","orange"]
2
3
def click(button):
4
car[0] += 1
5
(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:
JavaScript
1
8
1
car = ["red","blue","white","green","orange"]
2
3
index = 0
4
def click(button):
5
global index
6
index += 1
7
car[index]
8
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.