2

So, I'm new to programming and I'm currently learning Python. While trying to make a simple game, I got stuck when I tried to change the variable, named user_answer to input("Please, answer with yes or no."). Basically this is my line of code else: user_answer = input("Please, answer with yes or no."). But for some reason I get an error, called "Local variable 'user_answer' value is not used". Could anyone help me with this? Thanks in advance!

Here's my full code:

def my_function():
    user_answer = input("Would you like to play a game?")
    if user_answer == "yes":
        print("Great! I have a number in my head from 1 to 10. Make a guess!")
    elif user_answer == "no":
        print("Oh, okay! Maybe next time?")
    else: user_answer = input("Please, answer with yes or no.")


my_function()
2
  • 5
    This is not a Python error, probably just a warning from your IDE. It's probably because you don't use user_answer anywhere beyond this point. Commented Aug 13, 2018 at 11:08
  • Please indicate which IDE you are using. Commented Aug 13, 2018 at 11:34

1 Answer 1

6

I'm guessing you're talking about an IDE inspection, not an actual runtime error? Are you using an IDE like PyCharm? If so, local variable ... value not used means that you are storing a value (input(...)) in a variable (user_answer) and then you're never using that value. But that is just as it should be, because it seems your program ends there; nothing is using the new value of user_answer.

Just ignore the warning and continue writing your program, making sure to use the new value of the variable, and the warning should disappear.

For context, the purpose of this warning is to catch programming mistakes early on. Suppose that I wanted to write a function that takes a list and appends the element in the middle to either end (front and back), and I wrote this:

def wrap_with_middle_element(sequence):
    mid = sequence[len(sequence)//2]
    sequence.append(element)
    sequence.insert(0, element)
    return sequence

You'll notice I didn't actually use the variable mid after defining it, but rather I used element. This could be because, for example, there is a global variable named element which the IDE suggested as code completion and I accepted absentmindedly, or because mid was previously named element, and in manually renaming it to mid (instead of using the IDE's functionality), I forgot to rename the other two appearances of the variable. (I know this seems kind of unlikely, but it's just to illustrate.) In this case the IDE will show the warning: Local variable 'mid': value is not used, since I'm defining mid but never using it. I'd quickly realise what was wrong and fix that (instead of finding out later when running the program).



Working example of OP's code

def my_function():
    user_answer = input("Would you like to play a game? ")

    while user answer not in ('yes', 'no'):
        user_answer = input("Please, answer with yes or no. ")

    if user_answer == "yes":
        print("Great! I have a number in my head from 1 to 10. Make a guess!")
    elif user_answer == "no":
        print("Oh, okay! Maybe next time? ")


my_function()

In this example, first we get the user input with user_answer = input("Would you like to play a game? ") and then we make sure that it's either "yes" or "no" with the loop

while user_answer not in ('yes', 'no'):
        user_answer = input("Please, answer with yes or no. ")

which terminates only when user_answer in ('yes', 'no') (or, equivalently user_answer == 'yes' or user_answer == 'no') evaluates to True.

Then you can continue with the rest of the program!

# if user_input...

Why it didn't work

With your previous code, the else statement executed when the user didn't input a valid answer (yes or no). But the problem was that, once the user inputted a new value (which could have also been invalid!), the program had nowhere to go (it had already "left behind" the if user_answer == "yes": print(...) code!).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer, but the problem is that I'd like the program to print when the answer become "yes" or "no", that's why I want to change the input of user_answer
@Manu Correct me if I'm wrong; with the last else statement, you want the program to make sure the user inputs either "yes" or "no" but not something else like "foo"?
Dude, you saved my life!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.