samuelklit
Posts: 13
Joined: Thu Jul 24, 2014 7:09 pm

Python error

Fri Jul 25, 2014 6:04 pm

Hi, iam pretty new to python and i wrote this code:

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8-*-
import RPi.GPIO as GPIO
import datetime
import time
GPIO.setwarnings(False)
#Blink
def blink():
   GPIO.output(4, GPIO.HIGH)
   time.sleep(10)
   GPIO.output(4, GPIO.LOW)
   time.sleep(1)
   return
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
timeh = datetime.datetime.now().hour
timem = datetime.datetime.now().minute
if (timeh == 0 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 1 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 2 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 3 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 4 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 5 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 6 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 7 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 8 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 9 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 10 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 11 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 12 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 13 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 14 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 15 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 16 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 17 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 18 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 19 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 20 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 21 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 22 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 23 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 24 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
GPIO.cleanup()
#!/usr/bin/python
# -*- coding: utf-8-*-
import RPi.GPIO as GPIO
import datetime
import time
GPIO.setwarnings(False)
#Blink
def blink():
   GPIO.output(4, GPIO.HIGH)
   time.sleep(10)
   GPIO.output(4, GPIO.LOW)
   time.sleep(1)
   return
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
timeh = datetime.datetime.now().hour
timem = datetime.datetime.now().minute
if (timeh == 0 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 1 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 2 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 3 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 4 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 5 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 6 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 7 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 8 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 9 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 10 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 11 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 12 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 13 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 14 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 15 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 16 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 17 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 18 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 19 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 20 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 21 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 22 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 23 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 24 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
GPIO.cleanup()
#!/usr/bin/python
# -*- coding: utf-8-*-
import RPi.GPIO as GPIO
import datetime
import time
GPIO.setwarnings(False)
#Blink
def blink():
   GPIO.output(4, GPIO.HIGH)
   time.sleep(10)
   GPIO.output(4, GPIO.LOW)
   time.sleep(1)
   return
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
timeh = datetime.datetime.now().hour
timem = datetime.datetime.now().minute
if (timeh == 0 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 1 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 2 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 3 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 4 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 5 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 6 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 7 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 8 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 9 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 10 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 11 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 12 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 13 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 14 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 15 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 16 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 17 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 18 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 19 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 20 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 21 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 22 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 23 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
    elif (timeh == 24 and timem == 00):
        blink()
        print (timeh +":" + timem + ":" + times)
GPIO.cleanup()

But it keep getting errors.....
WHATS WRONG??? :?:

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python error

Fri Jul 25, 2014 6:14 pm

samuelklit wrote:WHATS WRONG??? :?:
That would depend on the errors you are getting. Post them, and someone is more likely to respond with the answer.

samuelklit
Posts: 13
Joined: Thu Jul 24, 2014 7:09 pm

Re: Python error

Fri Jul 25, 2014 6:23 pm

The error is

Code: Select all

pi@raspberrypi ~ $ sudo python Lystimeblink.py
  File "Lystimeblink.py", line 21
    elif (timeh == 1 and timem == 00):
                                     ^
IndentationError: unindent does not match any outer indentation level

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Python error

Fri Jul 25, 2014 7:02 pm

samuelklit wrote:The error is

Code: Select all

pi@raspberrypi ~ $ sudo python Lystimeblink.py
  File "Lystimeblink.py", line 21
    elif (timeh == 1 and timem == 00):
                                     ^
IndentationError: unindent does not match any outer indentation level
You asked "What is wrong?" The error message has told you exactly what is wrong. You need to understand that we are not here to fix problems in your code when the interpreter is telling you exactly what is wrong with your code.

Have you tried using google to find out what the error message means ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python error

Fri Jul 25, 2014 7:04 pm

Ah. Python is quite strict regarding indentation, it's not just for show. All lines at the same level of 'nesting' must have the same indentation. Typical practice is to use 4 spaces for each level (no tabs). ifs and elifs need to line up (same indentation). So something like:

Code: Select all

if (timeh == 0 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 1 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Python error

Fri Jul 25, 2014 7:13 pm

Douglas6 wrote:Ah. Python is quite strict regarding indentation, it's not just for show.
it is not "quite strict" about it, the indentation and layout is a fundamental part of the language syntax.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Python error

Fri Jul 25, 2014 7:23 pm

PeterO wrote:
Douglas6 wrote:Ah. Python is quite strict regarding indentation, it's not just for show.
it is not "quite strict" about it, the indentation and layout is a fundamental part of the language syntax.
PeterO
Perhaps simply 'strict' ("inflexibly maintained or adhered to") would have been more proper, if less conversational.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Python error

Tue Jul 29, 2014 12:56 am

And of course because you're doing exactly the same in each of the if/elif branches (and you've covered all 24 hours), you can replace

Code: Select all

if (timeh == 0 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 1 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 2 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 3 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 4 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 5 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 6 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 7 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 8 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 9 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 10 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 11 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 12 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 13 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 14 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 15 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 16 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 17 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 18 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 19 and timem == 00):
    blink()
     print (timeh +":" + timem + ":" + times)
elif (timeh == 20 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 21 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 22 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 23 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
elif (timeh == 24 and timem == 00):
    blink()
    print (timeh +":" + timem + ":" + times)
with just

Code: Select all

if timem == 00:
    blink()
    print (timeh +":" + timem + ":" + times)

Return to “Python”