Home > Blockchain >  flag change in python while loop
flag change in python while loop

Time:06-11

I want to send a request to an api in python using requests library. when I want to call the api first time, I should send a paramter but after the successful first call the parameter should not send. How can I handle this situation?? Example code:

        while True:
            firstTime = True -> This is the first time parameter
            params = {
                "accountNo": self.DEPOSIT,
                "lastTranKey": None if firstTime is True else "",
                "fromDate": fromDate,
                "toDate": toDate,
                "pageSize": length,
            }
            response = requests.post(
                url="",
                json=params,
                timeout=50
            )
            if response.status_code == 200:
                firstTime = False

CodePudding user response:

You may put firstTime before the loop, to do it once

firstTime = True
while True:
    params = {
        "accountNo": self.DEPOSIT,
        "lastTranKey": None if firstTime else "",
        "fromDate": fromDate,
        "toDate": toDate,
        "pageSize": length,
    }
    response = requests.post(
        url="",
        json=params,
        timeout=50
    )
    if response.status_code == 200:
        firstTime = False

CodePudding user response:

firstTime = True
while True:
    # do stuff
    firstTime = False
  • Related