我对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
答案 0 :(得分:0)
你好,我不确定我是否完全理解这个问题,但希望能有所帮助。
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)