“UnboundLocalError:赋值前引用的局部变量'input'”

时间:2014-01-01 21:08:36

标签: python variables input

当我运行我的代码时,我遇到了这些错误:

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

UnboundLocalError:赋值前引用的局部变量'input'

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:

2 个答案:

答案 0 :(得分:4)

您正在为

中的变量input分配字符串
y_n = input = ("Do you want to save these changes?\ny/n\n")

input现在的值为'Do you want to save these changes?\ny/n\n'

但是,您也在调用

中的内置函数input
linechoice = input("What password do you want to delete?:\n")

考虑更改变量的名称以避免这些冲突。

查看该计划的背景,您可能期待

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

而不是

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

答案 1 :(得分:0)

如果您收到此错误,请致电 输入()

  

UnboundLocalError:赋值前引用的局部变量'input'

比你应该检查你的运行时python解释器是否为3.x ,如果你假设它是2.x。

在python 3.6上执行时发生了这个错误:

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

所以我摆脱了这个,而是使用:

from builtins import input