-2

How to break the while loop, when the condition is true?

I tried using break under an if statement, but it's not working.

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()

I expect that the loop should stop when the if condition passes.

Habebit
  • 957
  • 6
  • 23
Sathish R
  • 3
  • 1

2 Answers2

4

here is a simplified version of what you could do:

condition = True
while condition:
    for i in range(10):
        print(i)
        if i == 3:
            condition = False
            break

break will break the for loop and setting condition = False will cause the while loop to stop.

and are you sure you want to conn = sqlite3.connect('database.db') in your while loop?

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
  • 1
    The while loop is the first line within a function so the OP may as well just `return` – Sayse Jul 02 '19 at 13:14
  • 1
    @Sayse there is a connection that still needs to be closed. returning may be a bit harsh... (the connection should be within a `with` context anyway....) but yeah, `return` works. – hiro protagonist Jul 02 '19 at 13:15
  • @hiroprotagonist it worked ! thanks, when i run the function it keeps on running, how to exit from it? – Sathish R Jul 02 '19 at 14:18
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
Sathish R
  • 3
  • 1