【问题标题】:How to fix : NameError: name 'password' is not defined如何修复:NameError:未定义名称“密码”
【发布时间】:2019-08-13 15:00:36
【问题描述】:

版本:Python 3.7.4

嗨,这是我的第一个代码。如果我正确输入所有内容,它会很好地工作。但是,如果我在某些点输入了错误的值,则会收到错误“NameError: name 'password' is not defined”。此外,如果您可以查看我的代码并向我提供一些关于如何使它变得更好的反馈,以及可能的解决方案,那就太棒了。请指导我尤达大师! :)

我尝试过的解决方案: 1.加括号,去掉括号,去掉冒号,加冒号。 2. 分配变量名。 3.阅读我正在学习的书,但根据书我的代码很好。 4.阅读类似的帖子,但他们的代码更高级,所以我无法理解代码和解决方案。 5.各种缩进。`

此错误发生在: - 如果我输入了错误的用户名值,则第 18 行。 - 第 30 行,如果我输入密码错误(NameError: name 'age' is not defined) - 第 43 行 NameError: name 'ans' is not

我的代码:

#Displays what I have learnt so far : Variable assignment, comparision operators, arithmetic operators, value operators, logical operators,

print('Welcome')
print('Enter Username')
username = input()
if username == 'Evan' :
    print('Enter Password')
else:
    print('Wrong Username. Program terminated.')

if username == 'Evan' :
    password = input()
    if password == 'Great' :
        print('Additional identification confirmation required.')
    else:
        print('Wrong password. Program terminated.')

if password == 'Great':
    print('What is your age?')
    years = (27)
    age = int(input())
    if age < years :
        print('Seriously?')
    elif age > years :
        print('-_-"')
    else:
        age == years
        print('Welcome Evan!')

if age == years:
    print('Name one of the RGB colours.')
    rgb = 'Red' or 'Green' or 'Blue'
    colours = input()
    if colours == rgb :
        print('Good')
    else:
        print('Wrong Answer')

if age == years :
    print('Type Rock & Roll.')
    answer = 'Rock & Roll'
    ans = input()
if ans != answer :
    print('Learn to type.')
else:
    print('Good job!')

if age == years:
    print('Let me show you some math for entertainment :D')
result = '= '
print('2+2')
print(result + str(int(2+2)))
print('')
print('3 - 2')
print(result + str(int(3-2)))
print('')
print('2 x 2')
print(result + str(int(2*2)))
print('')
print('7 / 3')
print(7/3)
print('')
print('3 modulus 7')
print(result + str(int(3%7)))
print('')
print('2 exponent 10')
print(result + str(int(2**10)))
print('')
print('23 floor division 9')
print(result + str(int(23//9)))

【问题讨论】:

  • 在 if 或 for 中包含变量时,最好始终使用空值预定义它们。

标签: python python-3.x


【解决方案1】:

您已将password限定到封闭的if 语句中。

if username == 'Evan' :
    password = input()

将其移出该块以使用它。

password = None
if username == 'Evan' :
    password = input()

【讨论】:

    【解决方案2】:

    问题是当你写“程序终止”时,实际上它并没有终止。程序会检查所有的 if,如果你写错了用户名,'password' 没有定义,它会崩溃。

    最好的解决方案是在函数中编写代码。这样,当你写“程序终止”时,你可以返回一个错误值并停止程序。

    def function_name():
        #Some code
        if username == "Evan":
            #Some code
        else:
            #You want to terminate the function in this case
            return 1
        #Some code
    #Function call
    function_name()
    

    【讨论】:

    • 感谢您的反馈。我是2天前开始的,所以我还没有学习你提到的功能。我会尝试一下。谢谢:)
    【解决方案3】:

    如果你真的想终止程序,你可以使用 quit() 函数,它会在你调用它时结束程序。

    我可以看到您重复了 if 语句,即它们检查的是相同的内容,因此请尝试嵌套 if else 语句。我还建议使用一些结构化代码,例如在程序开始时声明您将要使用的所有变量并将它们初始化为 None

    【讨论】:

    • 感谢您的回复。我大约 2 天前才开始学习,所以我还没有达到 quit() 功能,但我会尝试一下。我对编程很陌生,所以我无法理解“嵌套 if else 语句”是什么意思。另外,“将它们初始化为”是什么意思。如果您能详细说明这些,我将不胜感激。
    • 道歉@LightSpeed。您可以搜索嵌套的 if-else 语句。初始化变量意味着当你声明一个变量时,比如说 username,你接受输入来填充它的值,即你将它“初始化”为用户给定的值。因此,当我说将其初始化为 None 时,如果您知道 None 是什么,您可以改为: username =None next line username = input() 。这是一个标准,并不总是这样做,因为它会使代码变长,而且当你有一百万个变量时,它会很乏味。但除了竞争性编码之外,在项目中,这被认为是一种很好的做法。希望这会有所帮助。
    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2017-08-30
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多