Python 2.7.14: NameError

时间:2018-02-03 10:00:40

标签: python python-2.7

password = "spam"
done = False
while done == False:
    user_password = input("Enter the password: ")
    if password == user_password:
        done = True
    else:
        print("That's not the correct password! I can't let you in.")
print("SECRET PASSWORD GRANTED.")

Result:

NameError                                 Traceback (most recent call last)
<ipython-input-11-a71bf0a53652> in <module>()
      2 done = False
      3 while done == False:
----> 4     user_password = input("Enter the password: ")
      5     if password != user_password:
      6         done = True

C:\Users\Michael\Anaconda2\lib\site-packages\ipykernel\ipkernel.pyc in <lambda>(prompt)
    162             self._sys_eval_input = builtin_mod.input
    163             builtin_mod.raw_input = self.raw_input
--> 164             builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
    165         self._save_getpass = getpass.getpass
    166         getpass.getpass = self.getpass

C:\Users\Michael\Anaconda2\lib\site-packages\ipykernel\ipkernel.pyc in <module>()

NameError: name 'hello' is not defined

Ultimately, it comes down to this. I find when I run the code and enter Password: spam -- results in error when I enter any other word, same error, but if I use the phrase "spam", it completes correctly.

I am thinking that should not be the behavior, so, I seek suggestions and advice, as I am very new at this. Thank you for your time!

0 个答案:

没有答案