名称错误:未定义全局名称“PIN”

2024-09-27 19:27:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到以下错误消息:

Traceback (most recent call last):
 File "/Volumes/KINGSTON/Programming/Assignment.py", line 17, in <module>
    Assignment()
 File "/Volumes/KINGSTON/Programming/Assignment.py", line 3, in Assignment

我的代码是: 在

^{pr2}$

如果有人知道我的错误并能解释,我将非常感谢你的帮助


Tags: inpy消息most错误linecallfile
1条回答
网友
1楼 · 发布于 2024-09-27 19:27:56

引发这个特定的错误是因为当您说result = PINPIN实际上并不存在。因为它不在引号中,所以Python假设它是一个变量名,但是当它去检查这个变量是什么时,它找不到任何东西,并引发NameError。当您修复这个问题时,prompt也会发生这种情况,因为您稍后将其称为Prompt。在

我不确定这是否是您的完整代码,所以我不确定其他问题可能是什么,但看起来您似乎正在使用result和{}来控制while循环。请记住,while循环会一直运行,直到它所检查的条件是False(或者如果您手动打破它),那么您可以从以下内容开始,而不是声明额外的变量:

def Assignment():
    # No need to declare the other variables as they are only used once
    tries = 2

    # Go until tries == 0
    while tries > 0:
        ok = raw_input('What is your PIN?')
        # Remember that the output of `raw_input` is a string, so either make your
        # comparison value a string or your raw_input an int (here, 1234 is a string)
        if ok == '1234':
            # Here is another spot where you may hit an error if menu doesn't exist
            result = menu
            # Assuming that you can exit now, you use break
            break
        else:
            print 'Incorrect, please try again'
            # Little shortcut - you can rewrite tries = tries - 1 like this
            tries -= 1

        # I'll leave this for you to sort out, but do you want to show them both
        # the 'Please try again' and the 'Maximum attempts' messages?
        if tries == 0:
            print 'You have used your maximum number of attempts. Goodbye.'

相关问题 更多 >

    热门问题