3

I have this code:

def delete():
    print("Welcome to the password delete system.")
    file = open("pypyth.txt", "w")
    output =  []
    linechoice = input("What password do you want to delete?:\n")
    if linechoice == "email":
        for line in file:
            if "Hotmail" != line.strip():
                output.append(line)
                print("Password " + linechoice + " deleted.")
                y_n = input = ("Do you want to save these changes?\ny/n\n")
                if y_n == "y":
                    file.close()
                    print("Change saved.")
                    input("Press enter to go back to menu")
                    main()
                else:
                    main()
    elif linechoice == "skype":
        for line in file:
            if "Skype" != line.strip():
                output.append(line)
                print("Password " + linechoice + " deleted.")
                y_n = input = ("Do you want to save these changes?\ny/n\n")
                if y_n == "y":
                    file.close()
                    print("Change saved.")
                    input("Press enter to go back to menu")
                    main()
                else:
                    main()
    else:

Why do I get an error like so?

    linechoice = input("What password do you want to delete?:\n")
UnboundLocalError: local variable 'input' referenced before assignment
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user3151805
  • 39
  • 1
  • 1
  • 2
  • You are assigning to `input` in the line `y_n = input = ("Do you want to save these changes?\ny/n\n")` and `y_n = input = ("Do you want to save these changes?\ny/n\n")`, overwriting the built-in function `input`. –  Jan 01 '14 at 21:12

2 Answers2

3

You are assigning a string to the variable input in

y_n = input = ("Do you want to save these changes?\ny/n\n")

input now has the value of 'Do you want to save these changes?\ny/n\n'

However, you are also calling the built-in function input in

linechoice = input("What password do you want to delete?:\n")

Consider changing the name of your variable to avoid these conflicts.

Looking at the context of the program, you are probably expecting

y_n = input("Do you want to save these changes?\ny/n\n")

instead of

y_n = input = ("Do you want to save these changes?\ny/n\n")
DanGar
  • 3,018
  • 17
  • 17
  • Doesn't explain the error however. It should result in `TypeError: 'str' object is not callable`. –  Jan 01 '14 at 21:19
  • 2
    @Nabla no it wouldn't, because the assignment happens *after* the reference - but an assignment anywhere in a function makes the name local throughout that function, hence the NameError. – Daniel Roseman Jan 01 '14 at 21:30
  • Great job of coding gumshoery! – ncmathsadist Feb 08 '19 at 20:23
  • 1
    You're not explaining the real reason behind the `UnboundLocalError` at all, which is that the name `input` becomes a local variable of a function when it is assigned a value anywhere in the function, and that a local variable cannot be read until it is assigned a value. – blhsing Aug 02 '21 at 02:26
-1

If you got this error, due to calling input():

UnboundLocalError: local variable 'input' referenced before assignment

than you should check whether your runtime python interpreter is 3.x, if you were assuming it is 2.x.

This Error happened to me while executing on python 3.6:

if hasattr(__builtins__, 'raw_input'): 
    input = raw_input
input()

So I got rid of this, and instead used:

from builtins import input
Noam Manos
  • 15,216
  • 3
  • 86
  • 85