有没有办法让 if 语句的条件加上前面的条件?



我有一个关于if elif语句的问题。当我编写一个 if elif 语句并且我希望它们按顺序和特定次数执行一个语句时,有没有比使用时间变量并在之后让所有条件都排长线更好的方法?

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

有没有办法让 if 语句的条件加上前面的条件加上一些东西,而不是在后面写一长串条件?

马比这样:

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

您好,我不确定我是否完全理解这个问题,但希望这有所帮助。

import time
value = 2
otherValue = 4
while True:
if value < otherValue:
value = value * 2
otherValue = otherValue * 2
print("The value variable and condition is now: " + str(value))
print("The otherValue variable and condition is now: " + str(otherValue))
time.sleep(1)

最新更新