gurgy11
Posts: 36
Joined: Fri Dec 26, 2014 5:20 am

What Can Be Used Instead of "while" In Python, NO Loops?

Fri Jan 02, 2015 9:58 pm

Is there something in Python that allows me to do something, like "while" but without looping?

Can I use variables from inside of "while" in this "alternative"?

Thank you! :D

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: What Can Be Used Instead of "while" In Python, NO Loops?

Fri Jan 02, 2015 10:03 pm

Basically a while loop is

Code: Select all

while condition:
   statements
If you don't want to loop it just becomes

Code: Select all

statements
What are you trying to achieve?

gurgy11
Posts: 36
Joined: Fri Dec 26, 2014 5:20 am

Re: What Can Be Used Instead of "while" In Python, NO Loops?

Fri Jan 02, 2015 10:09 pm

joan wrote:Basically a while loop is

Code: Select all

while condition:
   statements
If you don't want to loop it just becomes

Code: Select all

statements
What are you trying to achieve?
I'm trying to send an email everytime an LED lights up.

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_PIR = 7 # Pin 26
GPIO_LED = 4 # Pin 7
GPIO.setup(GPIO_LED,GPIO.OUT)
GPIO.setup(GPIO_PIR,GPIO.IN)

print " Press Ctrl & C to Quit"

try:
    while GPIO.input(GPIO_PIR)==1:
        Motion = 0
    print " Ready"

    Motion = 0

    while True:
        if GPIO.input(GPIO_PIR)==1:
            if Motion == 0:
                print " Motion detected!"
                Motion = 1
            GPIO.output(GPIO_LED, True)
        else:
            if Motion == 1:
                print " Clear "
                Motion = 0
            GPIO.output(GPIO_LED, False)
            import smtplib

            content = 'example stuff'
            mail = smtplib.SMTP('smtp.gmail.com',587)
            mail.ehlo()
            mail.starttls()
            mail.login('EMAIL','PASSWORD')
            mail.sendmail('EMAIL','RECEIVER_EMAIL',content)
            mail.close()
            
except KeyboardInterrupt:
    print " Quit"
    GPIO.cleanup()

Notice where I try to send an email? It just loops, and sends me TONS of email! Haha!

Any example on how I can get it to send me only ONE email only once the LED Lights up, and not loop continuously?

Thanks!

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: What Can Be Used Instead of "while" In Python, NO Loops?

Fri Jan 02, 2015 10:12 pm

All you need to do is put another while in the while loop which waits until the led turns off, and then continues.
There are 10 types of people: those who understand binary and those who don't.

gurgy11
Posts: 36
Joined: Fri Dec 26, 2014 5:20 am

Re: What Can Be Used Instead of "while" In Python, NO Loops?

Fri Jan 02, 2015 10:14 pm

kusti8 wrote:All you need to do is put another while in the while loop which waits until the led turns off, and then continues.
Okay, I'll try that. My original understanding was that "while" loops.

gurgy11
Posts: 36
Joined: Fri Dec 26, 2014 5:20 am

Re: What Can Be Used Instead of "while" In Python, NO Loops?

Fri Jan 02, 2015 10:17 pm

kusti8 wrote:All you need to do is put another while in the while loop which waits until the led turns off, and then continues.
Sorry, but could you show me exactly where to put it?

(I'm completely new to this sort of stuff ;) )

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: What Can Be Used Instead of "while" In Python, NO Loops?

Fri Jan 02, 2015 10:22 pm

You could store the previous state of the LED, and only send an email if the state changes to 'on'.

Code: Select all

last_state=false

while true:
    new_state=GPIO input

    if new_state and not last_state:
        send email

    last_state=new_state

Return to “Python”