2012-11-02 137 views
0

我收到以下错误消息:NameError:全局名称“PIN”没有定义

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 

我的代码是: 如果有谁知道在哪里

def Assignment(): 
    prompt = 'What is your PIN?' 
    result = PIN 
    error = 'Incorrect, please try again' 
    retries = 2 
    while result == PIN: 
     ok = raw_input(Prompt) 
     if ok == 1234: 
      result = menu 
     else: 
      print error 
      retries = retries - 1 

     if retries < 0: 
      print 'You have used your maximum number of attempts. Goodbye.' 

Assignment(): 

会很感激的帮助不大我错了,可以解释

+2

什么是程序,你想做什么?你有什么试图解决这个问题?请给我们多一点继续,然后出现错误消息,一些代码和背后的轻拍以“拥有它”。 –

回答

0

这个特殊错误是因为当你说result = PIN时,PIN实际上不存在。由于它不在引号内,因此Python假定它是一个变量名,但是当它去检查那个变量等于什么时,它找不到任何东西并且引发了NameError。当你解决这个问题时,它也会发生在prompt之后,因为你稍后将它称为Prompt

我不知道这是否是您的完整代码或没有,所以我不知道其他问题可能是什么,但它看起来像你正在使用resultPIN控制你的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.' 
相关问题