Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Every time I run it, it says return outside function. What do I do?

What I have tried:

chars = "ACGT"

def neighbors(pattern, d):
    assert(d <= len(pattern))

if d == 0:
    return [pattern]

r2 = neighbors(pattern[1:], d-1)
r = [c + r3 for r3 in r2 for c in chars if c != pattern[0]]

if (d < len(pattern)):
    r2 = neighbors(pattern[1:], d)
    r += [pattern[0] + r3 for r3 in r2]

return r
Posted
Updated 17-May-18 23:18pm
v2

If you get an error message you don;t understand, Google it: return outside function - Google Search[^] and follow a few links.
It's pretty obvious if you just read the error message!
 
Share this answer
 
In python the indentation is important. Everything that belongs in the definition of neighbors should be indented at least once.
 
Share this answer
 
Quote:
Every time I run it, it says return outside function. What do I do?

With Python, indentation matters, it is the structure of your program, it is not just presentation. Need to be extra careful with it.
Python
chars = "ACGT"

def neighbors(pattern, d):
    assert(d <= len(pattern))

    if d == 0:
        return [pattern]
    
    r2 = neighbors(pattern[1:], d-1)
    r = [c + r3 for r3 in r2 for c in chars if c != pattern[0]]
    
    if (d < len(pattern)):
        r2 = neighbors(pattern[1:], d)
        r += [pattern[0] + r3 for r3 in r2]
    
    return r
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900