我认为由于空白而在python中出现语法错误

时间:2011-07-12 13:56:37

标签: python

我仍然对编写python代码的方式感到困惑,因为我习惯使用带有brakets的java代码,并且我无法完全理解缩进概念。所以这是一个产生语法错误的代码,所以任何人都可以帮忙吗?提前谢谢

def SendSMS2 (phone_number,sms_text):

print 'made it to def' 
res = MDM.send('AT+CMGS='+ phone_number + '\r',5)
n=0
while n < 20:
    res=MDM.receive(1)
    if res.find(">") >-1:
        print n
        break
        else:
            n = n +1

res = MDM.send(sms_text, 10)
if res != -1:
    print 'Text sent ok'

res = MDM.sendbyte(0x1a,10)
n=0
while n < 20:
    res=MDM.receive(1)
    if res.find("+CMGS") >-1:
        print n
        break
        if res.find("ERROR")>-1:
            print 'sms ok'
            else:
                print 'not good' 
                else:
                    n = n +1


print 'def completed *******************'

5 个答案:

答案 0 :(得分:3)

def语句之后的所有代码都应缩进,如下所示:

def SendSMS2 (phone_number,sms_text):

    print 'made it to def' 
    res = MDM.send('AT+CMGS='+ phone_number + '\r',5)
    n=0
    while n < 20:
        res=MDM.receive(1)
        if res.find(">") >-1:
            print n
            break
        else:
            n = n +1

    res = MDM.send(sms_text, 10)
    if res != -1:
        print 'Text sent ok'

    res = MDM.sendbyte(0x1a,10)
    n=0
    while n < 20:
        res=MDM.receive(1)
        if res.find("+CMGS") >-1:
            print n
            break
        else:
            n = n +1

        if res.find("ERROR")>-1:
            print 'sms ok'
        else:
            print 'not good' 



    print 'def completed *******************'

修改else语句也应该与if匹配

答案 1 :(得分:1)

定义函数时,应该是函数一部分的所有代码都需要比以“def func_name(arguments)”开头的行缩进一级。

此外,在撰写if else语句时,以else开头的行应缩进到与其对应的if行相同的级别。应该是这些块的一部分的任何代码都应该缩进一个级别。

如果你可以使用花括号,你可以使用Python的缩进。它们都用于创建代码块。类似于ifelsewhile语句的内容都用于启动代码块,可以使用花括号或空格来显示该块中包含的内容:

if (condition) {
this is an if block
it ends with a curly bracket
}
else {
this is an else block
it ends with a curly bracket
}

在python中,我们使用空格:

if condition:
    this is a python if block
    it ends when we move back a level of indentation
else:
    this is the corresponding else block
    it ends when we move back a level of indentation

因此,要从使用括号的语言移动到python,只需在开始括号中开始缩进,并在放入右括号时停止缩进。还有一点(see here),但这应该让你开始。

答案 2 :(得分:0)

认为你的功能应该按照以下方式修改(希望我使用了正确的修改):

def SendSMS2 (phone_number, sms_text):
    print 'made it to def'
    res = MDM.send('AT+CMGS=' + phone_number + '\r', 5)
    n = 0
    while n < 20:
        res = MDM.receive(1)
        if res.find(">") > -1:
            print n
            break
        else:
            n = n +1

    res = MDM.send(sms_text, 10)
    if res != -1:
        print 'Text sent ok'

    res = MDM.sendbyte(0x1a,10)
    n = 0
    while n < 20:
        res = MDM.receive(1)
        if res.find("+CMGS") > -1:
            print n
            if res.find("ERROR") > -1: 
                print 'not good'               
            else:
                print 'sms ok'
            break
        else:
            n = n +1                   

    print 'def completed *******************'

答案 3 :(得分:0)

# the line after a line ending in a colon is indented an extra level.
# when you are done with indented block of code dedent back to the 
# previous level
def SendSMS2 (phone_number,sms_text):
    # increased indent after the def
    print 'made it to def' 
    res = MDM.send('AT+CMGS='+ phone_number + '\r',5)
    n=0
    while n < 20:
        res=MDM.receive(1)
        if res.find(">") >-1:
            print n
            break
        # else belongs on same level of its corresponding if
        else:
            n = n +1

    res = MDM.send(sms_text, 10)
    if res != -1:
        print 'Text sent ok'

    res = MDM.sendbyte(0x1a,10)
    n=0
    while n < 20:
        res=MDM.receive(1)
        # python we would write this as: 
        # if "+CMGS" in res:
        if res.find("+CMGS") >-1:
            print n
            break
            # you'll never reach this line because of the break statement!
            # if "ERROR" in res:
            if res.find("ERROR")>-1:
                print 'sms ok'
            else:
                print 'not good' 
        else:
            n = n +1


    print 'def completed *******************'

答案 4 :(得分:0)

你的缩进都被打破了。此外,确实存在for个循环:

def SendSMS2 (phone_number, sms_text):
    print 'made it to def'
    res = MDM.send('AT+CMGS=%s\r' % phone_number, 5)
    for n in range(20):
        res = MDM.receive(1)
        if res.find(">") > -1:
            print n
            break
    res = MDM.send(sms_text, 10)
    if res != -1:
        print 'Text sent ok'
    res = MDM.sendbyte(0x1a, 10)
    for n in range(20):
        res = MDM.receive(1)
        if res.find("+CMGS") > -1:
            print n
            break
        if res.find("ERROR") > -1:
            print 'sms ok'
        else:
            print 'not good'
    print 'def completed *******************'