0

我正在尝试复制这个Instructable,而且我对 Python 还是很陌生。

当我尝试运行该 Python 代码时,我不断得到Unexpected Indent ErrorsUnexpected Indent Blocks.

我已经查看了程序中的缩进。如果我尝试删除它们,如果我再次检查程序,我仍然会在另一行得到错误。

任何帮助,将不胜感激。这是Instructable的代码。

#******************************************#
# Tweet-a-Pot by Gregg Horton 2011 #
# Please email changes or #
# updates to greggawatt@instructables.com #
# *****************************************#

 ##Import Libraries

 import twitter
 import serial
 import time

 ##authenticate yourself with twitter
 api = twitter.Api(consumer_key='consumerkeyhere', consumer_secret='consumersecrethere', access_token_key='accesskey', access_token_secret='accesssecret')

 ##set to your serial port
 ser = serial.Serial('/dev/ttyUSB0', 19200)

 ## check serial port
 def checkokay():
 ser.flushInput()
 time.sleep(3)
 line=ser.readline()
 time.sleep(3)

 if line == ' ':
 line=ser.readline()
 print 'here'
 ## Welcome message
 print 'Welcome To Drip Twit!'
 print 'Making Coffee..'
 def driptwit():
 status = [ ]
 x = 0

 status = api.GetUserTimeline('X') ##grab latest statuses

 checkIt = [s.text for s in status] ##put status in an array

 drip = checkIt[0].split() ##split first tweet into words

 ## check for match and write to serial if match
 if drip[0] == '#driptwit':
 print 'Tweet Recieved, Making Coffee'
 ser.write('1')
 elif drip[0] == '#driptwitstop': ##break if done
 ser.write('0')
 print 'stopped, awaiting instructions.'
 else:
 ser.write('0')
 print 'Awaiting Tweet'


 while 1:
 driptwit() ## call driptwit function
 time.sleep(15) ## sleep for 15 seconds to avoid rate limiting
4

2 回答 2

2

老兄,Python 是缩进敏感的!您的整个代码无效。

#******************************************#
# Tweet-a-Pot by Gregg Horton 2011 #
# Please email changes or #
# updates to greggawatt@instructables.com #
# *****************************************#

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='consumerkeyhere', consumer_secret='consumersecrethere', access_token_key='accesskey', access_token_secret='accesssecret')

##set to your serial port
ser = serial.Serial('/dev/ttyUSB0', 19200)

## check serial port
def checkokay():
    ser.flushInput()
    time.sleep(3)
    line=ser.readline()
    time.sleep(3)

    if line == ' ':
        line=ser.readline()
        print 'here'
        ## Welcome message
        print 'Welcome To Drip Twit!'
        print 'Making Coffee..'
def driptwit():
    status = [ ]
    x = 0

    status = api.GetUserTimeline('X') ##grab latest statuses

    checkIt = [s.text for s in status] ##put status in an array

    drip = checkIt[0].split() ##split first tweet into words

    ## check for match and write to serial if match
    if drip[0] == '#driptwit':
        print 'Tweet Recieved, Making Coffee'
        ser.write('1')
        elif drip[0] == '#driptwitstop': ##break if done
        ser.write('0')
        print 'stopped, awaiting instructions.'
        else:
        ser.write('0')
        print 'Awaiting Tweet'


while 1:
    driptwit() ## call driptwit function
    time.sleep(15) ## sleep for 15 seconds to avoid rate limiting
于 2013-11-13T13:44:39.527 回答
0

删除空格,然后在每一行添加正确的缩进。祝你好运。

我无聊了,为你做了压痕

#******************************************#
# Tweet-a-Pot by Gregg Horton 2011         #
# Please email changes or                  # 
# updates to greggawatt@instructables.com  #
# so i can keep it updated         #
# *****************************************#

##Import Libraries

import twitter
import serial
import time

##authenticate yourself with twitter
api = twitter.Api(consumer_key='your key here', consumer_secret='your key here', access_token_key='your key here',
                  access_token_secret='your here here')

##set to your serial port
ser = serial.Serial('/dev/ttyUSB0', 19200)

## check serial port
def checkokay():
    ser.flushInput()
    time.sleep(3)
    line = ser.readline()
    time.sleep(3)

    if line == ' ':
        line = ser.readline()
    print 'here'

## Welcome message
print 'Welcome To Drip Twit!'


def driptwit():
    status = []
    x = 0

    status = api.GetUserTimeline('yourusername') ##grab latest statuses

    checkIt = [s.text for s in status] ##put status in an array

    drip = checkIt[0].split() ##split first tweet into words

    ## check for match and write to serial if match
    if drip[0] == '#driptwit':
        print 'Tweet Recieved, Making Coffee'
        ser.write('1')
    elif drip[0] == '#driptwitstop': ##break if done
        ser.write('0')
        print 'stopped, awaiting instructions.'
    else:
        ser.write('0')
        print 'Awaiting Tweet'


while 1:
    driptwit() ## call driptwit function
    time.sleep(15) ## sleep for 15 seconds to avoid rate limiting
于 2013-11-13T13:40:41.317 回答