Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:49:47+00:00 2026-06-09T17:49:47+00:00

I have the following Python code: def find_words(letters): results = set() def extend_prefix(w, letters):

  • 0

I have the following Python code:

def find_words(letters):
    results = set()

    def extend_prefix(w, letters):
        if w in WORDS: results.add(w)
        if w not in PREFIXES: return
        for L in letters:
            result = extend_prefix(w + L, letters.replace(L, "", 1))
            results = results | result

    extend_prefix('', letters)
    return results

print find_words("ABCDEFGH")

When I run it, I get the following error:

Traceback (most recent call last):
  File "ExtendPrefix.py", line 44, in <module>
    print find_words("ABCDEFGH")
  File "ExtendPrefix.py", line 41, in find_words
    extend_prefix('', letters)
  File "ExtendPrefix.py", line 38, in extend_prefix
    result = extend_prefix(w + L, letters.replace(L, "", 1))
  File "ExtendPrefix.py", line 38, in extend_prefix
    result = extend_prefix(w + L, letters.replace(L, "", 1))
  File "ExtendPrefix.py", line 35, in extend_prefix
    if w in WORDS: results.add(w)
UnboundLocalError: local variable 'results' referenced before assignment

It apparently can’t find results in a recursive call to extend_prefix. Why is this and how can I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:49:49+00:00Added an answer on June 9, 2026 at 5:49 pm

    Because you’re assigning results inside of a nested function, Python assumes you’re using a locally scoped variable and throws up at line 35 even though it’s a valid name in a higher scope. If you were only reading the variable and not writing to it, it will oftentimes work on the higher namespace object. But as soon as an assignment operator appears, you jump to local namespace.

    From Python scopes/namespaces:

    A special quirk of Python is that – if no global statement is in
    effect – assignments to names always go into the innermost scope.
    Assignments do not copy data — they just bind names to objects.

    To get around this, the easiest is to pass the variable you want use into the function header:

    def extend_prefix(w, letters, results):
            if w in WORDS: results.add(w)
            if w not in PREFIXES: return
            for L in letters:
                extend_prefix(w + L, letters.replace(L, "", 1), results)
    

    Also the way you wrote the function, you weren’t returning a set, so the results = results | result would have blown up with results being None Type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following python code using the twisted API. def function(self,filename): def results(result):
I have the following code in Python: def point_to_index(point): if point not in points:
I'm working in Python. I have the following code: while not is_suffix(pattern[:k], pattern[:q]): k
Suppose I have the following python code: def outer(): string = def inner(): string
Kinda knew to Python: I have the following code: def printCSV(output, values, header): 63
I have the following IronPython code. class Hello: def __init__(self): pass def add(self, x,
I have the following IronPython code. class Hello: def __init__(self): pass def add(self, x,
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
So I have the following code GAE code in python and it is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.