0

I am still confused in the way of writting a python code as i am used to java codes with brakets and i cant fully understand the indentation concept. So this is a code that produces a syntax error so any one can help please ? Thank You in advance

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 *******************'
3
  • 2
    You can put the Traceback please? Commented Jul 12, 2011 at 13:58
  • 1
    To make the indentation part clearer: think of each level of indentation as being wrapped with another set of brackets. It's fairly easy to get used to if you indent all your bracketed blocks to begin with when writing Java code. You did use good programming style in Java, right? Commented Jul 12, 2011 at 14:03
  • 1
    You may try to read PEP 8 (Code Style Guide) python.org/dev/peps/pep-0008 Commented Jul 12, 2011 at 14:08

5 Answers 5

3

All of the code after the def statement should be indented, like this:

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 *******************'

Edit: The else statements should also match up with the ifs

Sign up to request clarification or add additional context in comments.

2 Comments

There is no way then 'if res.find("ERROR")>-1: ' to be executed since it placed after break
Its not really clear what the intent is, but I've edited it to appear like it probably should
1

When you define a function, all of the code that should be part of the function needs to be indented one level further than the line starting with "def func_name(arguments)".

Additionally, when writing if else statements, the line beginning with else should be indented to the same level as it's corresponding if line. Any code that should be a part of those blocks should be indented one more level.

If you can work with curly brackets, you can work with Python's indentation. They're both used to create blocks of code. Things like an if, else, or while statement are all used to start a block of code, and either curly brackets or whitespace can be used to show what is contained in that block:

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
}

In python, we use whitespace:

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

So, to move from a language that uses brackets to python, simply start indenting where you would stick in an opening bracket, and stop indenting when you would put in a closing bracket. There's a little more to it then that (see here) but that should get you started.

1 Comment

i thought it is from the Code Sample, the if/else blocks are also a problems
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 *******************'

Comments

0

Think that your function should be modified the following way(hope i have used correct indetation):

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 *******************'

Comments

0

Yor indentation is all broken. Also, there do exist for loops:

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 *******************'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.