Tweetable Coffee Pot- Python缩进错误

时间:2013-11-13 13:40:13

标签: python indentation

我正在尝试复制这个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

2 个答案:

答案 0 :(得分:2)

Dude,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

答案 1 :(得分: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