← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #154447]: How to end a while True inside a function

 

Question #154447 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/154447

    Status: Open => Answered

RaiMan proposed the following answer:
if you say

test()

it prints "done" - believe me ;-)

--- comment 1:
if you want to do it this way, the else is not necessary:

i = 0
while True:
   if i == 3: break
   i += 1
print "done"

--- comment 2:
since this is needed often, Python has something for that

for i in range(4):
    print i
print "done"

as you can see this gives you 0, 1, 2, 3 in turn.

the possible usages
:
range(max-value) # starts with 0, steps by 1
range(start-value, max-value) # steps by 1
range(start-value, max-value, step-value) # steps by step-value

# the max value is not given into the loop, but stops it
# appropriate negative values work as expected

so range(1, 20,  7)
gives you 1, 8, 15 

--- since list (array) index counting starts with 0 the following works as expected:
aList = [1, 2, 3, 4, 5]
for i in range(len(aList)):
    print aList[i]

you use this instead of 
for item in aList:
   print item
if you need the index inside the loop.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.