2

There is something wrong with my python code. I am a beginner in python.

def gen_qr_text(acc_id,amount):
    pp_acc_id = ""
    pp_amount = ""
    pp_chksum = ""

if len(acc_id) == 15:
    pp_acc_id = "0315" + acc_id
elif acc_id.length() == 13:
    pp_acc_id = "0213" + acc_id
elif acc_id.length() == 10:
    pp_acc_id = "01130066" + acc_id.substring(1)
else:
    return "null"

if not amount:
    pp_amount = format("54%02d%s", amount.length(), amount)

    pp_str = "00020101021129370016A000000677010111"
    + pp_acc_id
    + "5303764"
    + pp_amount
    + "5802TH"
    + "6304"
    pp_chksum = crc16.checksum(pp_str);
    pp_str += pp_chksum;
    return pp_str

Error says it's SyntaxError: 'return' outside function. What's the problem with this code. By the way i convert the code from java to python. Is that everything okey in my code? I edit my code here but still there are some error

2
  • 2
    Something went wrong with your code formatting. Consult Markdown help - Code and Preformatted Text and edit your post. Commented Nov 16, 2017 at 16:27
  • 3
    Please indent your code we can't know which return statement is throwing the error Commented Nov 16, 2017 at 16:27

2 Answers 2

2

You need to properly indent your code. From your question I'm guessing this would resolve your issue:

    import crc16

    def gen_qr_text(acc_id,amount):
        pp_acc_id = ""
        pp_amount = ""
        pp_chksum = ""

        if len(acc_id) == 15:
          pp_acc_id = "0315" + acc_id
        elif acc_id.length() == 13:
          pp_acc_id = "0213" + acc_id
        elif acc_id.length() == 10:
          pp_acc_id = "01130066" + acc_id.substring(1)
        else:
          return "null"

        if not amount:
          pp_amount = format("54%02d%s", amount.length(), amount)

        pp_str = "00020101021129370016A000000677010111" + pp_acc_id + "5303764" + pp_amount + "5802TH" + "6304"
        pp_chksum = crc16.checksum(pp_str);
        pp_str += pp_chksum;
        return pp_str
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, its almost done but something error "pp_chksum = crc16.checksum(pp_str);" in this line. Can you help me out to calculate crc16 with checksum in that line?
undefined name 'crc16'
you need to import that module import crc16
2

Python is what is called "whitespace-sensitive": it matters how stuff is indented. For example, it expects the contents inside an if-clause to be indented below that statement:

if True or False:
    execute_this_function()

This will not work:

if True or False:
execute_this_function()

The same applies to return statements: they should be inside the function they apply to, i.e. that you are returning from:

def my_method():
    return True

Again, this will not work and raise the error you are getting:

def my_method():
return True

Thus, the solution is to make sure your returns are indented correctly. Of course, the same applies to the rest of the code!

Edit Based on the modification of your OP, this is the indentation you require; I also fixed the .length() you copied over from Java, it seems:

def gen_qr_text(acc_id,amount):
    pp_acc_id = ""
    pp_amount = ""
    pp_chksum = ""

    if len(acc_id) == 15:
        pp_acc_id = "0315" + acc_id
    elif len(acc_id) == 13:
        pp_acc_id = "0213" + acc_id
    elif len(acc_id) == 10:
        pp_acc_id = "01130066" + acc_id.substring(1)
    else:
        return "null"

    if not amount:
        pp_amount = format("54%02d%s", len(amount), amount)

        pp_str = "00020101021129370016A000000677010111"
        + pp_acc_id
        + "5303764"
        + pp_amount
        + "5802TH"
        + "6304"
        pp_chksum = crc16.checksum(pp_str);
        pp_str += pp_chksum;
        return pp_str

4 Comments

True or False that's a strange example.
Not as strange as True and False.
I'm not sure if he wants the rest of the code after assigning pp_amount indented. I figured the reason he's checking is to reassign pp_amount . But I could be wrong.
That through me off as well. Along with all the .length()s

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.