我有一个动态变量 temp_x ,其值不断变化。我希望有一个if条件,在经过特定的持续时间后执行,并且具有 temp_x
的特定值import time
while True:
temp_y.ValueConverted = 1
time.sleep(20)
if temp_x == 0:
temp_y.ValueConverted = 3
break
print ('Done')
如果条件允许,我无法弄清楚如何在20秒内实现等待。所以我把它放在外面。所以我试图将20秒的等待时间纳入if条件。
注意:可能是在20秒过后,temp_x的值可能不是' 0'。它必须等到触及' 0'然后执行if语句。所以基本上算法应该先等待20秒,然后等到temp_x的值为' 0',然后执行if语句。
我很确定我做错了。如果有人能指引我走向正确的方向。它很棒。
编辑1:
while True:
temp_x.ValueConverted = 1
time.sleep(20)
while temp_y.VaueConverted != 0:
temp_x.ValueConverted = 3
break
错误: Traceback(最近一次调用最后一次): 文件" C:\ Users ...... \ Experiment_001 \ Python Scripts \ Script11211.py",第66行,in 而temp_x.VaueConverted!= 0: 文件" C:\ Program Files(x86)\ Python27 \ lib \ site-packages \ win32com \ client \ dynamic.py",第530行, getattr 引发AttributeError("%s。%s"%(自我。用户名,attr)) AttributeError:.VaueConverted
答案 0 :(得分:0)
你的代码没有任何问题,除了它比它有点复杂。以下内容对我来说更像 on-the-money :
import time
temp_y.ValueConverted = 1 # initial value
while temp_x != 0: # until it becomes 0 we...
time.sleep(20) # ...wait
temp_y.ValueConverted = 3 # and then we update the original temp_y
print('Done')
最后,请注意我们使用while
块阻止执行,因此初始temp_y.ValueConverted
值没有任何影响。我们可以跳过初始化并在完成检查后将其定义为。