while loop as long as value doesn't change

Question:

I need to solve a newbie IQ 70 problem, but can’t find a way to do it. I need to quit the while loop if the block_number changes. Can’t be that difficult!

while await asyncio.sleep(interval, True):

   block_number= get_current_block_number()

   while block_number stays the same:
   
       insert data into table

   else: do something else now     #block_number has changed
Asked By: Soapp

||

Answers:

Make a copy of block_number in another variable, then compare those two values in the while loop:

block_number = something
original_block_number = block_number

while block_number == original_block_number:
    # do stuff that might change block_number
Answered By: John Gordon

Based on the information you just passed, see if the code below solves your problem:

import asyncio

async def main():
    interval = 1
    block_number = get_current_block()

    while True:
        await asyncio.sleep(interval, True)

        while True:
            # Here we can check if the number has been modified
            current_block_number = get_current_block()
            if current_block_number != block_number:
                break

            insert_data_into_table()

        do_something_else()

        # And here we update the block number
        block_number = current_block_number

asyncio.run(main())
Answered By: rzz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.