473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 "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
5 23873
jlm699
314 100+
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
2,851 Expert Mod 2GB
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
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
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
314 100+
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

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

Similar topics

1
by: Anand | last post by:
I am calling a python script from LabVIEW. This is achieved by making a dll call to python22.dll. It works perfectly well for most of my code. I now want to throwup dialog boxes from python. It...
1
by: Newgene | last post by:
Hi, group, I am trying to dynamically add a method to class by following this post: ...
2
by: seberino | last post by:
I'm trying run a homegrown profiler on some Python code. Rather than apply profiler wrapper to ALL functions by hand.... Is there a low level Python function I can override to modify how ALL...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
1
by: melinda | last post by:
How do you set up a MDI interface so that one of the MDI forms can be dragged outside of the parent form. For example, in Visual Studio, you can do this with some of the windows. Is this MDI? ...
3
by: brianlum | last post by:
Hi, I was wondering if anyone knew of a built in Python function that will check if an item is a member of a list, i.e., if item i is a member of list l. I read of a function "in" but I can't...
12
by: Jon Slaughter | last post by:
is there any way to simply add an attribute like feature to a function/method definition? say I create a function like function Test() { //.... }
4
by: John Nagle | last post by:
Python's own loader searches "sys.path" for module names, but is there some function that makes that search functionality accessible to Python programs? I need the absolute pathname of a module,...
6
by: grbgooglefan | last post by:
I am creating functions, the return result of which I am using to make decisions in combined expressions. In some expressions, I would like to inverse the return result of function. E.g....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.