我正在寻找使用 WHILE 循环或 FOR 循环的不同方法来解决我目前面临的问题。
我正在寻找待处理(检查)循环
我有代码发送请求并接收输出,这是报告创建时的空字段或日期。
想法是做这样的事情:
WHILE generatedDate is None:
run the script which gets information if it None
IF generatedDate is not None
Another bit of code which will download the report which just created
代码应该是这样吗?
while x is None
#Do stuff
if x is not None
#Do other stuff
答案正是我所寻找的,抱歉没有提供完整的详细信息。
import time
#This part I made to access API and receive up to date JSON file
def get_data():
data=None #Initially no data
return data
#This section was modified to def download_report
def treat_data(data):
#do the stuff when you got data, in your case download the report
pass
#Check if data received
generatedData=get_data()
#Loop for pinging when data will be filled in.
while generatedData==None:
#Pingin text
print "waiting data"
generatedData=get_data()
#Wating time between checks
time.sleep(0.25)
#download_report call after exiting the loop (after field become not empty)
treat_data(generatedData) #report generated
答案 0 :(得分:1)
也许是这样的?
import time
#this function will return None if no data, and return the data if you got data
def get_data():
#here we do stuff to verify if we got data
data=None #we suppose there is no data
return data
def treat_data(data):
#do the stuff when you got data, in your case download the report
pass
#check first if we got data
generatedData=get_data()
#then waiting until you get data
while generatedData==None:
#check again if we got data
print "waiting data"
generatedData=get_data()
#wait 0.25s between two check
time.sleep(0.25)
#here we assume we got data (because we leave the while loop)
#so we treat the data
treat_data(generatedData)
由于我们没有实现func get_data,因此该脚本将返回:
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
waiting data
[...]
答案 1 :(得分:-2)
虽然应该是小写,它应该有一个类似的条件 while(var == true): ...