简体   繁体   中英

while loop as long as value doesn't change

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!

For every transaction in the blockchain:

   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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM