Home > OS >  else not executed after a while in Python
else not executed after a while in Python

Time:08-05

I am not sure why but my else statement is not executed after a while, despite of my while statement is true so it executes 'pass', and after that should handle the false statement within the else but it does not.

describe = client_sc.describe_provisioned_product(Id=pp_id)
describe_status = describe['ProvisionedProductDetail']['Status']
while describe_status != 'AVAILABLE':
   pass
else:
   create_access_key = client_iam.create_access_key(UserName='Tom')

CodePudding user response:

while describe_status != 'AVAILABLE':
    pass

If describe_status is not equal to "AVAILABLE", that loop will run forever.

It feels like you meant to have an if condition, not a while loop...

CodePudding user response:

Well, it seems that you don't change the describe_status as it suggested

describe = client_sc.describe_provisioned_product(Id=pp_id)
describe_status = describe['ProvisionedProductDetail']['Status']
while describe_status != 'AVAILABLE':
   # If you make changes here - the while loop will work properly
else:
   create_access_key = client_iam.create_access_key(UserName='Tom')

As an example of work, try this:

describe = client_sc.describe_provisioned_product(Id=pp_id)
describe_status = describe['ProvisionedProductDetail']['Status']
counter = 0
while describe_status != 'AVAILABLE':
   counter  = 1
   if counter == 5:
       describe_status = 'AVAILABLE'
       # hence the statement satisfies the criteria
else:
   create_access_key = client_iam.create_access_key(UserName='Tom')

CodePudding user response:

while describe_status != 'AVAILABLE':
   pass

This will run perpetually. You are essentially saying, while describe_status is not available, pass. This loop will run forever if describe_status does not get updated.

It becomes a forever loop, a loop that will never stop. You will have to stop the loop before your else codes will execute.

You could try this:

while describe_status != 'AVAILABLE':
      # Update describe_status here.
      describe = client_sc.describe_provisioned_product(Id=pp_id)
      describe_status = describe['ProvisionedProductDetail']['Status']

      # You could put a wait time here.

else:
     create_access_key = client_iam.create_access_key(UserName='Tom')

  • Related