By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,316 Members | 2,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,316 IT Pros & Developers. It's quick & easy.

Python SyntaxError "Outside Function"

P: 3
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 "SyntaxError: '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]))
Jul 8 '08 #1
Share this Question
Share on Google+
5 Replies


jlm699
100+
P: 314
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 "SyntaxError: '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)
Expand|Select|Wrap|Line Numbers
  1. def PC(key, src, decryp=True):
  2.     sum1 = sum2 = keyXorVal = 0
  3.     if len(key) != 16:
  4.         print 'Bad key length!'
  5.         return None
  6.     wkey = []
  7.     for i in xrange( 8 ):
  8.         wkey.append( ord( key[ i*2 ] ) << 8 | ord( key[ i*2+1 ] ) )
  9.  
Jul 8 '08 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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 "SyntaxError: '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.
Expand|Select|Wrap|Line Numbers
  1. def PC1(key, src, decryption=True):
  2. sum1 = 0;
  3. sum2 = 0;
  4. keyXorVal = 0;
  5. if len(key)!=16:
  6. print “Bad key length!”
  7. return None
  8. wkey = []
  9. for i in xrange(8):
  10. wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
Please use code tags when posting code. You have no indentation. Proper indentation is very important. It should look like this:
Expand|Select|Wrap|Line Numbers
  1. def PC1(key, src, decryption=True):
  2.     sum1 = 0
  3.     sum2 = 0
  4.     keyXorVal = 0
  5.     if len(key)!=16:
  6.         print "Bad key length!"
  7.         return None
  8.     wkey = []
  9.     for i in xrange(8):
  10.         wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
Jul 8 '08 #3

P: 3
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.
Jul 8 '08 #4

P: 3
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 - "SyntaxError '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.

Expand|Select|Wrap|Line Numbers
  1. for j in xrange(8):
  2.      wkey[j] ^= keyXorVal;
  3.      dst+=chr(curByte)
  4.      return dst
  5.  
Jul 8 '08 #5

jlm699
100+
P: 314
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=False): " 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:
Expand|Select|Wrap|Line Numbers
  1. for j in xrange(8):
  2.     wkey[j] ^= keyXorVal;
  3.     dst+=chr(curByte)
  4. return dst
  5.  
Jul 8 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.