0

My program has the following scturcutre

  1. program takes screenshot 2, program looks for condition a. if condition a is not met, it need to go back to point 1
  2. program looks for condition b. if condition b is not met, it need to go back to point 1
  3. program looks for condition c. if condition c is not met, it need to go back to point 1 ... etc

There are around 20 additional conditions and whenever one of them is not met, the program should return to the starting point. With a goto statement this could be easily solved. However, in Python this is not an option. Any suggestions are appreciated how this can be implemented elegantly.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Nickpick
  • 6,163
  • 16
  • 65
  • 116
  • Please show us the code you've written and we can let you know the most logical way. – stevieb Aug 07 '15 at 18:33
  • 2
    There are situations when `goto` and labels are a good solution (or at least not worse than any other), using `goto` for loops is not one of them. You *do* know about the [`continue` statement](https://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops)? – Some programmer dude Aug 07 '15 at 18:33
  • 3
    use `while 1:` and `continue`. – thebjorn Aug 07 '15 at 18:34
  • 1
    You should really read the [official Python tutorial](https://docs.python.org/3.4/tutorial) (any tutorial for a modern programming language, really) if `goto` is still in your programming toolbox. – TigerhawkT3 Aug 07 '15 at 18:48
  • I would look into State Machine based solution, possibly. Hard to tell wo more concrete code examples from your end. Here's one so entry to get you started: http://stackoverflow.com/questions/2101961/python-state-machine-design. – JL Peyret Aug 07 '15 at 19:07
  • and re. goto being always useless, I beg to differ. Maybe 5% of the time, I would like to have it available. Things like *simple* error conditions and retries for example (not the OP's question however). Try/except and loops do not always map directly to a plain English description of desired execution flow, sometimes a goto would have been better. The point is moot in Python however, there is no goto. – JL Peyret Aug 07 '15 at 19:10
  • It depends on how you define "useful." If you define it as "a feature that I might choose to use sometimes over more modern alternatives," then yes, `goto` is useful. If, however, you define it as "a feature that should have been included in the language because it is a good way to do some things and not an anti-pattern at all," then it's not useful. Python doesn't have moderately practical things like incrementors and TCO; I don't see why it would have something like `goto`. – TigerhawkT3 Aug 07 '15 at 19:32
  • while 1: and continue will do the trick I think and at the end break. thanks – Nickpick Aug 07 '15 at 19:59
  • 1
    Possible duplicate of [The equivalent of a GOTO in python](http://stackoverflow.com/questions/18863309/the-equivalent-of-a-goto-in-python) –  Nov 29 '15 at 22:23

1 Answers1

0

You should give us the code, or at least an example showing what the structure really is.

Maybe you could do so:

take screenshot
condition = (condition a) and (condition b) and ... and (condition z)

while (not condition):
    take screenshot
    condition = (condition a) and (condition b) and ... and (condition z)
Unknown
  • 61
  • 5
  • This is close to what I would like to do, however if condition b is not met, I don't want to program to check for condition c. It should go back directly to do another screenshot and restart from the beginning with first checking condition a. then b and finally c only if both previous conditions were true. – Nickpick Aug 07 '15 at 19:45
  • 1
    As it is written, it will work as you want: boolean expressions with `and` are evaluated through short-circuit evaluation (also called lazy evaluation) in Python. So if `condition b` is not met, Python evaluates the boolean expression `condition` to False without checking the value of `condition c` (if `condition b` is met, the program evaluates `condition c`, and so on...). – Unknown Aug 07 '15 at 20:10