Home > other >  Understanding how a try except logic works within two while loops
Understanding how a try except logic works within two while loops

Time:07-14

I'm having trouble understanding the mechanism of my code. I want to retrieve the realtime price data of a crypto from an exchange and print it every second, while creating a retry mechanism that catches an exception when it occurs.

while True:
    try_cnt = 10
    while try_cnt > 0:
        try:
            price = get_current_price(symbol)           # function imported from elsewhere
            print(price)
            time.sleep(1)
            try_cnt = 0
            break
        except Exception as e:
            print(e)
            try_cnt -= 1

This is how I have it set up but I'm having trouble follow the logic. This is how I see it as of now. After the code has been run:

  1. Price is retrieved
  2. try_cnt is set to 0 and the second while loop is broken
  3. The next second, try_cnt is reset to 10 and price is retrieved
  4. try_cnt is set to 0 again and the second while loop is broken
  5. repeat of this process

Am I understanding this correctly?

EDIT: the reason why I have a while loop outside is that I have other things that are going on at specific times besides the part I mentioned here. The part that I mentioned is supposed to be running all the time every second. For example,

while True:
    if now.hour == 0 and now.minute == 0 and now.second == 0:
        A = functionA(x,y)
    try_cnt = 10
    while try_cnt > 0:
        try:
            price = get_current_price(symbol)           # function imported from elsewhere
            print(price)
            time.sleep(1)
            try_cnt = 0
            break
        except Exception as e:
            print(e)
            try_cnt -= 1

CodePudding user response:

I think that you are understanding everything just fine, but the code itself is a little silly and redundant. For example, setting try_cnt to 0 and then following that with break is pointless - both lines do the same thing. And unless there is some significance to the variable try_cnt that is not present within the code you posted, its existence seems pretty much useless to me. Here's code that does the exact same thing as the code above, but without the redundancies:

while True:
    try:
        price = get_current_price(symbol)
        print(price)
        time.sleep(1)
    except Exception as e:
        print(e)

In response to your edit, the while True loop encompassing everything makes a lot more sense. However, this means that your code isn't doing what you want it to do. The outer while loop ensures that even if try_cnt reaches 0, try_cnt will just be reset to 10 on the next pass and it will be as if no Exceptions ever occurred (besides the ten error messages in the console). If you want that inner while loop to stop executing when ten Exceptions occur, you need to place the try_cnt = 10 statement outside of both while loops like so:

try_cnt = 10
while True:
    if now.hour == 0 and now.minute == 0 and now.second == 0:
         A = functionA(x,y)
    while try_cnt > 0:
        try:
            price = get_current_price(symbol)           
            print(price)
            time.sleep(1)
            try_cnt = 10
            break
        except Exception as e:
            print(e)
            try_cnt -= 1
  • Related