samedi 23 mai 2020

is there a way to have the if statements' condition to have the previous condition plus something?

I have a question about the if elif statement. When I write an if elif statement and I want them to execute one at the time, in order and a specific amount of times, is there a better way to do it than have a time variable and have all the conditions be a long line afterward?

example in python:

time = 0   
wait1 = 1   
wait2 = 4   
wait3 = 2

while True:
    if time < wait1:
        print("1")
    elif time < wait1 + wait2:
        print("2") 
    elif time < wait1 + wait2 + wait3:
        print("3")
    time += 1

is there a way to have the if statements' condition to have the previous condition plus something, instead of writing the long line of condition afterward?

maby something like:

time = 0    
wait1 = 1    
wait2 = 4    
wait3 = 2    

while True:
    if time < wait1:
        print("1")
    elif time < (previous condition) + wait2:
        print("2") 
    elif time < (previous condition) + wait3:
        print("3")
    time += 1

Aucun commentaire:

Enregistrer un commentaire