subway117016
Posts: 2
Joined: Thu Jun 07, 2018 2:39 pm

Help why unexpected indent ?

Fri Jun 22, 2018 4:38 pm

Code: Select all

from time import sleep
import urllib
import time,RPi.GPIO as GPIO
from time import strftime

def fetch_thing(url, params, method):
  if method=='POST':
    f = urllib.urlopen(url, params)
  else:
    f = urllib.urlopen(url+'?'+params)
  return (f.read(),f.code)
GPIO.setmode(GPIO.BOARD)   
GPIO.setwarnings(False)
button1=16
button2=12   
GPIO.setup(16,GPIO.IN) 
GPIO.setup(12,GPIO.IN) 
GPIO.setup(22, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
room=False                 
bathroom=False                 
while True:
	now= strftime('%Y-%m-%d %H:%M:%S')                 
        if GPIO.input(button1)==0:   
                if room==False:
                        GPIO.output(22,True)                       
                        room=True 
                        print "room on"
                        print "time",now
                        sleep(0.5)   
		content, response_code=fetch_thing('http://127.0.0.1/gpio.php','id=1&number=1&datetime=0&submit=1','GET')	         
				else: 
                        GPIO.output(22,False) 
                        room=False    
                        print "room off"
                        print "time",now           
                        sleep(0.5)
		content,response_code=fetch_thing('http://127.0.0.1/gpio.php','id=1&number=1&datetime=0&submit=0','GET') 
        if GPIO.input(button2)==0:
                if bathroom==False:
                        GPIO.output(18,True)
                        bathroom=True
                        print "bathroom on"
                        print "time",now
                        sleep(0.5)
		content, response_code=fetch_thing('http://127.0.0.1/gpio.php','id=0&number=2&datetime=0&submit=1','GET')
				else:
                        GPIO.output(18,False)
                        bathroom=False
                        print "bathroom off"
                        print "time",now
                        sleep(0.5)
		content, response_code=fetch_thing('http://127.0.0.1/gpio.php','id=0&number=2&datetime=0&submit=0','GET')   

User avatar
Paeryn
Posts: 2169
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Help why unexpected indent ?

Fri Jun 22, 2018 5:02 pm

subway117016 wrote:
Fri Jun 22, 2018 4:38 pm

Code: Select all

                if room==False:
                        GPIO.output(22,True)                       
                        room=True 
                        print "room on"
                        print "time",now
                        sleep(0.5)   
		content, response_code=fetch_thing('http://127.0.0.1/gpio.php','id=1&number=1&datetime=0&submit=1','GET')	         
				else: 
                        GPIO.output(22,False) 
                        room=False    
                        print "room off"
                        print "time",now           
                        sleep(0.5)
		content,response_code=fetch_thing('http://127.0.0.1/gpio.php','id=1&number=1&datetime=0&submit=0','GET') 
        if GPIO.input(button2)==0:
                if bathroom==False:
                        GPIO.output(18,True)
                        bathroom=True
                        print "bathroom on"
                        print "time",now
                        sleep(0.5)
		content, response_code=fetch_thing('http://127.0.0.1/gpio.php','id=0&number=2&datetime=0&submit=1','GET')
				else:
                        GPIO.output(18,False)
                        bathroom=False
                        print "bathroom off"
                        print "time",now
                        sleep(0.5)
		content, response_code=fetch_thing('http://127.0.0.1/gpio.php','id=0&number=2&datetime=0&submit=0','GET')   
The 2 lines after the sleep(0.5) both times look to be incorrectly indented. The first line (beginning content, response_code= is indented at the level of the if statement so you have left that if block, then the next line is else: which is indented even further than any matching if. Python requires correct indentation.
She who travels light — forgot something.

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

Re: Help why unexpected indent ?

Fri Jun 22, 2018 5:02 pm

Looks to me you're mixing tabs and spaces. That will cause problems in Python...
This is how it looks like in my editor
rpi-indent.png
rpi-indent.png (25.2 KiB) Viewed 183 times
The 'now' line has a single tab and the 'if' line has 8 spaces.

Return to “Python”