Random but Specific Dict question - Plant Simulator

Hey guys!
Just a random specific python question up ahead:

I’m trying to make a little plant simulator program where each time you water it (press a specific button) the plant grows (sprite changes).

I was thinking of sorting the sprites in a dict, so the key would be a number that can be incremented and the value would be the path with the sprite. Each time the key in the dict got incremented (the button was pressed) it would change the sprite accordingly.

I managed to increment the values in the dict like so:

stages = {
    1 : 'test' , #leaf
    2 : 'blah' , #Bud
    3 : 'bleep' , #Flower
    4 : 'bloop' , #Fruit

}

print(stages[1])
print(stages[1 + 1]) #State 2, works

but I also want the program to know which state the plant is currently in without me having to code in repetitive if statements such as:

if state = 1 and button is pressed then change state to = 2 and show different sprite.
if state = 2 and button is pressed then change state to = 3 and…

So I made a variable to store the current state but I can’t seem to find a way to increment the value stored in it.


stages = {
    1 : 'test' , #leaf
    2 : 'blah' , #Bud
    3 : 'bleep' , #Flower
    4 : 'bloop' , #Fruit

}
currentState = stages[2]

if currentState == stages[2]: #Will work as: if currentState is 2 and button is pressted then...
    print(stages[+1]) #State[3], doesn't work, prints stage[1]

I was hoping the last line of code would work something like:

apples = 1
apples +=1
#apples = 2

And have tried a few different methods based on the same concept but that doesn’t seem to be possible…
Do I really need to manually put in the if statements for each state? That just seems inefficient, what if have many states? Or also want to decrease values instead of just increasing?

I’d love for some input or ideas on how to solve this.

Thank you for reading!

I think you need to use currentState as a number then get the name of the state using that:

currentState = 1
print("plant is at", state[currentState]);

currentState = currentState + 1
print("plant is now at", state[currentState]);

Try that.

Hey thanks!
Also wanted to note for other people looking out there that there is a built in function:
enumerate()

https://book.pythontips.com/en/latest/enumerate.html

:slight_smile:

Ahhhh, that’s what you were trying to do. Can you take the time to look at your original question, and then rewrite a reply here with what your question should have been based on what you learned? I’m curious what you were actually trying to do since enumerate is pretty different from what you asked.

Thanks!