-3
words = []
words_needed = 0


def input_words():
    inputWords = input('Please input more words that you want to play with.').lower()
    words.append(inputWords)
    words_needed += 1
    while words_needed < 5:
        input_words()
    else:
        words_needed >= 5
        input_SS = input('Do you want to continue adding words?')
        if input_SS == 'yes':
            input_words()
        elif input_SS == 'no':
            end

def Start_up():
    start_question = input('Do you want to add your own words to the list?')
    if start_question == 'yes':
        input_words()
    elif start_question == 'no':
        pre_words = (*words in a list*)
        words.extend(pre_words)


Start_up()

When I run this segment of code it either runs off forever of brings me back an error of;

Traceback (most recent call last):
  File "F:\A453\Code\Python Hangman\Hangman.py", line X, in <module>
    Start_up()
  File "F:\A453\Code\Python Hangman\Hangman.py", line Y, in Start_up
    input_words()
  File "F:\A453\Code\Python Hangman\Hangman.py", line Z, in input_words
    words_needed += 1
UnboundLocalError: local variable 'words_needed' referenced before assignment

Im fairly new to coding so any help would be appreciated

Geneio
  • 1
  • 1

2 Answers2

2

Let me explain the problem to you

The problem is with the statement

words_needed += 1

It expands to

words_needed =  words_needed + 1

So it creates a local variable inside your function, however you are trying to access it's value when you are doing words_needed + 1 and is thus throwing an error.

You have to options left

  • Standard and Accurate way.
    Define your function as def input_words(words_needed): passing the words_needed as parameter and wherever you are calling the function call it as input_words(words_needed)

  • Bad and insecure way.
    Add a line, global words_needed at the start of your input_words function

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    This question gives some more details: http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – dshepherd Mar 29 '15 at 18:43
0

When you call words_needed+=1, you are trying to access the variable words_needed in the local scope, where it is not defined. Therefore, you should pass in and return words_needed, so it is accessible everywhere:

words = []
words_needed = 0


def input_words(words_needed):
    inputWords = input('Please input more words that you want to play with.').lower()
    words.append(inputWords)
    words_needed += 1
    while words_needed < 5:
        input_words()
    else:
        words_needed >= 5
        input_SS = input('Do you want to continue adding words?')
        if input_SS == 'yes':
            input_words()
        elif input_SS == 'no':
            return words_needed
    return words_needed

def Start_up():
    start_question = input('Do you want to add your own words to the list?')
    if start_question == 'yes':
        words_needed = input_words(words_needed)
    elif start_question == 'no':
        pre_words = (["words", "in", "a", "list"])
        words.extend(pre_words)


Start_up()
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76