"UnboundLocalError:赋值之前引用的局部变量'input'" [英] "UnboundLocalError: local variable 'input' referenced before assignment"

查看:133
本文介绍了"UnboundLocalError:赋值之前引用的局部变量'input'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行代码时,出现以下错误:

When I run my code I get these errors:

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

UnboundLocalError:赋值之前引用了本地变量'input'

UnboundLocalError: local variable 'input' referenced before assignment

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:

推荐答案

您正在将字符串分配给变量input in

You are assigning a string to the variable input in

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

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

但是,您还在

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")

代替

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

这篇关于"UnboundLocalError:赋值之前引用的局部变量'input'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆