sikuli-driver team mailing list archive
-
sikuli-driver team
-
Mailing list archive
-
Message #15694
Re: [Question #219984]: Decrease for loop by one
Question #219984 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/219984
Status: Open => Answered
Brian Parma proposed the following answer:
There are 3 issues I see with your code snippet:
1) "x - 1 = x" is an invalid statement. Expressions go on the right
side of '=' and variables on the left, you would need to do 'x = x - 1'.
I assume this was just a typo on this question, though, as it wouldn't
have even run with that error.
2) This isn't really an error, but a better way to do the check (by better i mean more Pythonic) is:
if decimal_value in myList:
Also, if you had a larger list, it would be faster to use a dictionary,
since they have O(1) lookups instead of O(n). For 12 it doesn't matter.
3) THIS IS THE MOST IMPORTANT: you can't change the x value in a loop as you've written it. The for statement iterates over items in a list (an iterable, actually). Range(0,10) simply produces the list [0,1,2....,9], and for each iteration, the 'current' element is stored in x. If you change x it has no effect on the next iteration, it is simply re-assigned to the next element in the list.
If you want to iterate an unknown number of times, or possibly repeat loops, it is probably better to use a while loop:
x = 0
while x < 10:
...
x = x + 1
or, if you know you are just going to quit when you have 10 elements in the list:
while len(myList) < 10:
...
Information on Python flow control:
http://docs.python.org/2/tutorial/controlflow.html
--
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.