在条件为真时如何中断while循环?
我尝试在if语句下使用break,但是它不起作用。
def proallocate():
while True:
conn = sqlite3.connect('database.db')
cur = conn.cursor()
pid = cur.execute("SELECT project_id FROM Upload WHERE status= ?", ("NULL",))
for pd in pid:
a = str(pd)
projectid = re.sub('[\(\),\{\}<>]', '', a)
url = "https://picture-us.amazon.com/colorauthor/view/"+projectid+""
print(url)
req = requests.Session()
resp = req.get(url, auth=HTTPKerberosAuth(mutual_authentication=False), verify="./amazon_dev_certs.pem")
soup = BeautifulSoup(resp.text, 'html.parser')
project_status_latest = soup.find_all('tr')[-1].get_text()
if ("Project Ready" in project_status_latest):
proid[0] = projectid
print(proid[0])
else:
cur.execute("UPDATE Upload SET status = ? WHERE project_id = ?", ("Reserved", projectid))
conn.commit()
我希望if条件通过时循环应该停止。
答案 0 :(得分:4)
这是您可以做的事情的简化版本:
condition = True
while condition:
for i in range(10):
print(i)
if i == 3:
condition = False
break
break
将中断for
循环,而设置condition = False
将导致while
循环停止。
,您确定要在conn = sqlite3.connect('database.db')
循环中while
吗?
答案 1 :(得分:0)
q = True
while q:
conn = sqlite3.connect('database.db')
cur = conn.cursor()
pid = cur.execute("SELECT project_id FROM Upload WHERE status= ?", ("NULL",))
for pd in pid:
a = str(pd)
projectid = re.sub('[\(\),\{\}<>]', '', a)
url = "https://picture-us.amazon.com/colorauthor/view/"+projectid+""
print(url)
req = requests.Session()
resp = req.get(url, auth=HTTPKerberosAuth(mutual_authentication=False), verify="./amazon_dev_certs.pem")
soup = BeautifulSoup(resp.text, 'html.parser')
project_status_latest = soup.find_all('tr')[-1].get_text()
if ("Project Ready" in project_status_latest):
proid[0] = projectid
q = False
break
else:
cur.execute("UPDATE Upload SET status = ? WHERE project_id = ?", ("Reserved", projectid))
conn.commit()
break
it worked