Python SyntaxError "Outside Function"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • riverhorus
    New Member
    • Jul 2008
    • 3

    Python SyntaxError "Outside Function"

    I'm new to Python. I got this Python script from a friend. When I run it, I get an error message for line 7, "return None" saying "SyntaxErro r: 'return' outside function" Can someone explain what the SyntaxError is and how to fix it?

    Here's the block of script. The error occurs in line 7.

    def PC1(key, src, decryption=True ):
    sum1 = 0;
    sum2 = 0;
    keyXorVal = 0;
    if len(key)!=16:
    print “Bad key length!”
    return None
    wkey = []
    for i in xrange(8):
    wkey.append(ord (key[i*2])<<8 | ord(key[i*2+1]))
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by riverhorus
    I'm new to Python. I got this Python script from a friend. When I run it, I get an error message for line 7, "return None" saying "SyntaxErro r: 'return' outside function" Can someone explain what the SyntaxError is and how to fix it?

    Here's the block of script. The error occurs in line 7.

    def PC1(key, src, decryption=True ):
    sum1 = 0;
    sum2 = 0;
    keyXorVal = 0;
    if len(key)!=16:
    print “Bad key length!”
    return None
    wkey = []
    for i in xrange(8):
    wkey.append(ord (key[i*2])<<8 | ord(key[i*2+1]))
    None of your code is indented.. Either that or it is and the formatting was lost when you didn't use code tags. Refer to the posting guidelines on information of how to use the code tags.

    Either way, this code is nonsense. Here's the fixed code with correct indentation and removal of unnecessary things (ie, semi-colons)
    [code=python]
    def PC(key, src, decryp=True):
    sum1 = sum2 = keyXorVal = 0
    if len(key) != 16:
    print 'Bad key length!'
    return None
    wkey = []
    for i in xrange( 8 ):
    wkey.append( ord( key[ i*2 ] ) << 8 | ord( key[ i*2+1 ] ) )
    [/code]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by riverhorus
      I'm new to Python. I got this Python script from a friend. When I run it, I get an error message for line 7, "return None" saying "SyntaxErro r: 'return' outside function" Can someone explain what the SyntaxError is and how to fix it?

      Here's the block of script. The error occurs in line 7.
      [code=Python]
      def PC1(key, src, decryption=True ):
      sum1 = 0;
      sum2 = 0;
      keyXorVal = 0;
      if len(key)!=16:
      print “Bad key length!”
      return None
      wkey = []
      for i in xrange(8):
      wkey.append(ord (key[i*2])<<8 | ord(key[i*2+1]))[/code]
      Please use code tags when posting code. You have no indentation. Proper indentation is very important. It should look like this:[code=Python]def PC1(key, src, decryption=True ):
      sum1 = 0
      sum2 = 0
      keyXorVal = 0
      if len(key)!=16:
      print "Bad key length!"
      return None
      wkey = []
      for i in xrange(8):
      wkey.append(ord (key[i*2])<<8 | ord(key[i*2+1]))[/code]

      Comment

      • riverhorus
        New Member
        • Jul 2008
        • 3

        #4
        My apologies. I didn't know about the code tags. I realized the indentations were important but didn't know how to display them properly. I'll try to do better next time.

        Anyway in the script the indentations are there but that hasn't affected the SyntaxError I keep getting saying the line "return None" is "outside function."

        Can you address that particular SyntaxError? I'll copy the suggested changes over to the script and see if fixes the problem.

        Comment

        • riverhorus
          New Member
          • Jul 2008
          • 3

          #5
          Your suggestions corrected problem in previous block of script, but I'm getting the same error in the following block in line 4. Same error as before - "SyntaxErro r 'return' outside function." What does "outside function" mean or indicate? I've tried modifying the script modeled on the changes previously recommended, but nothing I do in terms of indentation helps. May I impose again to ask for help.

          I hope I've used the CODE tags properly this time.

          Code:
          for j in xrange(8):
               wkey[j] ^= keyXorVal;
               dst+=chr(curByte)
               return dst

          Comment

          • jlm699
            Contributor
            • Jul 2007
            • 314

            #6
            Originally posted by riverhorus
            What does "outside function" mean or indicate?
            It means the the return statement is not within a function. In your previous example you had declared "def PC1(key, src, decryption=Fals e): " with a block of code indented below this line. As long as code is at least one level of indentation past the def statement, it is considered within the function PC1 .

            You cannot use return unless you are returning a value or exiting a function as such. If you give us more code we may be able to pin point where you are losing the scope of your function.

            On another note I don't think you'll want a return statement within your for loop as it will exit on the first iteration... you'll probably want something like this:
            Code:
            for j in xrange(8):
                wkey[j] ^= keyXorVal;
                dst+=chr(curByte)
            return dst

            Comment

            Working...