-1

I've a scenario where I've called particular block of code after a set of actions. I used go-to and label in Python. It works as expected.

Is there any other better alternative for this?

This is Python code for automation using Squish-for-QT.

label .mylabel 

while (cond1):
        print("inside cond1")
        Function1(x,y,z)
else:
    if (object.exists("obj1")):
        Screen1 = waitForObject("obj1")
        print ("Inside Screen1")

        while (Screen1.visible):
            Function1(a,b,c)
        else:
            goto .mylabel 
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
madhu_e
  • 5
  • 3
  • Possible duplicate of [Is there a label/goto in Python?](https://stackoverflow.com/questions/438844/is-there-a-label-goto-in-python) – Makoto Jun 14 '17 at 16:37
  • 2
    _"It works as expected."_ - It does? Python doesn't have native support for goto's. Are you using some library? – Christian Dean Jun 14 '17 at 16:38
  • Yes it worked. I used the "entrian go-to" library. Now, I've edited the code to use the "while True" solution suggested by "wphicks" – madhu_e Aug 10 '17 at 18:53

2 Answers2

1

You could define the code above into a function and use some basic recursion like so

def action():
    while (cond1):
        print("inside cond1")
        Function1(x,y,z)
    else:
        if (object.exists("obj1")):
            Screen1 = waitForObject("obj1")
            print ("Inside Screen1")

            while (Screen1.visible):
                Function1(a,b,c)
            else:
                action()

Generally using recursion is more common place. Hope this helps

cbolles
  • 475
  • 5
  • 17
  • 1
    This will eventually overflow the stack if it hits the recursive call too many times, and I think you may have gotten the indentation wrong on the last `else`. – user2357112 Jun 14 '17 at 16:49
  • The code in the question flows out of the loop if `object.exists("obj1")` tests false, and reenters the outer loop if `Screen1.visible` tests false. Your code has the opposite behavior. – user2357112 Jun 14 '17 at 16:53
1

In this particular case, wrapping the whole thing in a while True will achieve the same behavior:

while True:
    while (cond1):
            print("inside cond1")
            Function1(x,y,z)
    else:
        if (object.exists("obj1")):
            Screen1 = waitForObject("obj1")
            print ("Inside Screen1")

            while (Screen1.visible):
                Function1(a,b,c)
        else:
            break

Checking this branch-by-branch:

If cond1 is met, we continually execute Function1(x, y, z)

Once cond1 is not met, we fall into else.

If obj1 exists, we wait for obj1, otherwise, we break out of the while True loop.

After waiting for obj1, we continue to run Function1(a,b,c) while Screen1 is visible, and then go back to the beginning of the while True loop (consistent with the original goto).

wphicks
  • 355
  • 2
  • 9