讀取一個 android 顯示模式監視器,它給出了插入解析度的值。如果回圈連續多次讀取“null”,我希望回圈中斷:
Display Mode: 720p60hz
Display Mode: 720p60hz
Display Mode: null
Display Mode: null
Display Mode: null
Display Mode: null
BREAK!
CODE
import time
import subprocess
while True:
z = subprocess.getoutput("adb shell cat /sys/class/display/mode")
time.sleep(.1)
print(f'Display Mode: {z}')
t1 = time.time()
t2 = time.time()
if z == 'null':
print(f't1 is :{t1}')
else:
continue
if z == 'null'
print(f't2 is :{t2}')
print('i am null')
if t2-t1 > .1:
break
uj5u.com熱心網友回復:
您的代碼有點難以閱讀,因為縮進被忽略了,并且縮進是 Python 語法的一部分。但這是我對您要查找的內容的最佳猜測:
import time
import subprocess
THRESHOLD_BAD_DISPLAY_READS = 10 # Set this to the number of bad reads in a row that you can tolerate
num_bad_display_reads = 0
while num_bad_display_reads < THRESHOLD_BAD_DISPLAY_READS:
display_mode = subprocess.getoutput("adb shell cat /sys/class/display/mode")
time.sleep(.1)
if display_mode == 'null' or display_mode is None:
num_bad_display_reads = 1 # Start counting up
else:
display_mode = num_bad_display_reads = 0 # Reset the count
#print(f'Display Mode: {display_mode}') # Uncomment this during debugging
# You have now left the display loop. Feel free to do something else.
我的猜測是,如果顯示恢復,您想重新進入回圈。如果這是真的,那么我建議您在另一個 SO 問題中發布一些關于此的細節。
注意:我改名z為display_mode.
uj5u.com熱心網友回復:
您上面的示例未顯示有效的 Python 代碼。
對于您想要的結果,您可以將計數器初始化為 0,每次獲得 null 時將其遞增,如果出現任何其他值,則將其重置回 0:
counter = 0
while True:
if value == 'null':
counter = 1
else:
counter = 0
if counter < 5:
# Code runs as usual here
else:
print("Too many null values in a row, halting")
break
uj5u.com熱心網友回復:
如果我很好地理解了您的代碼,那么當 z 接收到空值時,您正試圖打破回圈。在 python 中,我們使用 None 來表示空值。
嘗試替換這一行:
if z == 'null'
對于這一行:
if z == None
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452298.html
