python运行出错
File "./DHT11.py", line 20 for i in range(0,500): ^ IndentationError: unexpected indent
如何解决
以下是全部代码:
#!/usr/bin/python import RPi.GPIO as GPIO import time def bin2dec(string_num): return str(int(string_num, 2)) data = [] GPIO.setmode(GPIO.BCM) GPIO.setup(4,GPIO.OUT) GPIO.output(4,GPIO.HIGH) time.sleep(0.025) GPIO.output(4,GPIO.LOW) time.sleep(0.02) GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) for i in range(0,500): data.append(GPIO.input(4)) bit_count = 0 tmp = 0 count = 0 HumidityBit = "" TemperatureBit = "" crc = "" try: while data[count] == 1: tmp = 1 count = count + 1 for i in range(0, 32): bit_count = 0 while data[count] == 0: tmp = 1 count = count + 1 while data[count] == 1: bit_count = bit_count + 1 count = count + 1 if bit_count > 3: if i>=0 and i<8: HumidityBit = HumidityBit + "1" if i>=16 and i<24: TemperatureBit = TemperatureBit + "1" else: if i>=0 and i<8: HumidityBit = HumidityBit + "0" if i>=16 and i<24: TemperatureBit = TemperatureBit + "0" except: print "ERR_RANGE" exit(0) try: for i in range(0, 8): bit_count = 0 while data[count] == 0: tmp = 1 count = count + 1 while data[count] == 1: bit_count = bit_count + 1 count = count + 1 if bit_count > 3: crc = crc + "1" else: crc = crc + "0" except: print "ERR_RANGE" exit(0) Humidity = bin2dec(HumidityBit) Temperature = bin2dec(TemperatureBit) if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0: print Humidity print Temperature else: print "ERR_CRC"
来自这个网址:
http://www.raspberrypi.org/forums/viewtopic.php?t=69427&p=505235
这个问题就变成了怎样把已有的代码,保证缩进正确的导入Raspberry Pi当中。
其实最好的办法是台式机上Notepad++编辑好了之后,FileZilla等SFTP工具直接把文件传到树莓派上边去……
我提供另外一种文件原样传递的办法吧。
稍复杂,但灵活一些,适合没有SFTP或不方便SFTP的时候:
wget 'http://paste.openstack.org/raw/82147/' -O dht11.py
下载下来就行了,保证所有缩进的样式传递完全原样。我总觉得程序根本就是完全乱七八糟的……
整个程序所有的缩进全部无道理的乱死,完全无法识别你的程序的结构、目的。要知道对于Python来说,程序不整齐就意味着程序功能本身的失败。我没法回答你,我相信别人也没法回答你,因为改这个东西根本无从下手。
题主可以简单的说一下你的程序是从哪里获得的,也许能有更好的建议。