bobdx_cool
Posts: 39
Joined: Fri Apr 04, 2014 11:23 pm

Exiting from a for and while loop

Sun Oct 26, 2014 11:25 pm

Code: Select all


def function1():

totalSteps = 1000
stepsDone = 0
    while (switch == 1 or stepsDone <= totalSteps):
            for x in range(0,4000):
                 GPIO.output(10,GPIO.HIGH)
                 if (switch ==0):
                    break
                 if (stepsDone==totalSteps):
                     break
                 GPIO.output(26,GPIO.HIGH)
                 time.sleep(.0008)
                 GPIO.output(26,GPIO.LOW)
                 time.sleep(.0008)
                 stepsDone=stepsDone+1
    GPIO.output(10,GPIO.LOW)
    stepsDone =0

The problem I am facing is that when the if (stepsDone==totalSteps): becomes true the program breaks out of the for loop, but not out of the while loop. What should I change for it break out of for and while loop and execute GPIO.output(10,GPIO.LOW).

DirkS
Posts: 10497
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Exiting from a for and while loop

Sun Oct 26, 2014 11:41 pm

because of the <= in the while statement it will not exit when the two variables are equal.
If you also want to exit if both are equal then you should use just 'less than' in the while statement:

Code: Select all

while (switch == 1 or stepsDone < totalSteps):
Gr.
Dirk.

User avatar
DougieLawson
Posts: 40497
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Exiting from a for and while loop

Sun Oct 26, 2014 11:50 pm

Python's break can only ever break out of the currently active loop (current level of indentation).

What are you trying to achieve with the stepsDone counter? If you run 4000 times round the for loop it will ALWAYS exceed total steps before the for loop is complete.

Also you may want to look at a truth table for the "switch == 1 or stepsDone <= totalSteps" conditions, consider turning that round so you check "switch == 0 AND stepsDone > totalSteps". With multiple conditions it's always a good idea to jot down a truth table to help visualise the logic you're writing (until you become an expert at think this out in your head).

try: except: & finally: exception handling allows you to "raise" an exception up to a higher level.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

billio
Posts: 71
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: Exiting from a for and while loop

Tue Oct 28, 2014 12:12 am

Your code is a little obtuse, is this what you meant to happen ?.
Set 10 high, then
Set 26 high and low very quickly for 1000 times or until the switch status is 0, then
Set 10 low

Code: Select all

def function1():
    totalSteps = 1000
    stepsDone = 0
    GPIO.output(10,GPIO.HIGH)
    while switch == 1 and stepsDone < totalSteps :
        GPIO.output(26,GPIO.HIGH)
        time.sleep(.0008)
        GPIO.output(26,GPIO.LOW)
        time.sleep(.0008)
        stepsDone=stepsDone+1
    GPIO.output(10,GPIO.LOW)

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Exiting from a for and while loop

Tue Oct 28, 2014 5:57 pm

Code: Select all

running=True
while running:
    for i in range(0,4000):
        do something...
        if switch==1:
            running=False
            break
Something like that should work.

Dave.
Apple say... Monkey do !!

User avatar
paddyg
Posts: 2602
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Exiting from a for and while loop

Wed Oct 29, 2014 9:36 pm

and/or the classic advice is to convert it into different functions which allows you to use 'return' to break out of whatever convoluted nested block you happen to be in.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”