473,804 Members | 3,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python SyntaxError "Outside Function"

3 New Member
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]))
Jul 8 '08 #1
5 23907
jlm699
314 Contributor
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)
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 Recognized Expert Moderator Specialist
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.
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
riverhorus
3 New Member
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
riverhorus
3 New Member
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.

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 Contributor
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:
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
2548
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 works just fine if i run the code once. but when i call the same piece of code the second time, wxPython.wx.miscc does not exist for some reason. I dont know how this gets deleted. I guess this happens during some kind of clean up. The code that...
1
3865
by: Newgene | last post by:
Hi, group, I am trying to dynamically add a method to class by following this post: http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/2ec2ad7a0a5d54a1/928e91be352c6bfc?q=%22new.code(%22+%22import+new&_done=%2Fgroup%2Fcomp.lang.python%2Fsearch%3Fgroup%3Dcomp.lang.python%26q%3D%22new.code(%22+%22import+new%26qt_g%3D1%26&_doneTitle=Back+to+Search&&d#928e91be352c6bfc But I got the Error like the below: ...
2
1619
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 functions are called? Chris
19
3583
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 one web presentation tier (Foo.WebFiles). The data tier shall only be accessible thru the business tier so I do NOT want a reference to the data tier in the presentation tier. In the business tier I have a class with the name CategoryItem that...
1
3558
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? Or how do you that in C#.net?
3
2066
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 seem to get that to work and finding pages for "python in" does not reveal very many relevant sources.
12
2384
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
1362
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, with the search being done exactly the same way "import" does it. The loader for "egg" files has this functionality, but I'd like to find out if there's a standard way to do this before looking into that source code. Also, it seems that the...
6
1764
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. function contains(source,search) will return true if "search" string is found in source string. I want to make reverse of this by putting it as: if ( ! contains(s1,s2) ): return 1
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.