[Tutor] Getting confusing NameError

Evert Rol evert.rol at gmail.com
Wed Aug 18 09:28:28 CEST 2010


> This is my first mail to this list so firstly thank you for reading and apologies in advance for any noob mistakes i may have inadvertantly made:). Ive only started learning python as my first programming language and its all going well so far, working my way through a couple of books one of which is programming python: for absolute beginners by Michael Dawson
>  
> In one of the code samples he supplies while doing loops ( namely if and else loops) im getting a NameError i dont understand and cannot find any help anywhere about it. here is the code he supplied and the error im getting
>  
> # Password
> # Demonstrates the if statement
>  
> print("Welcome to System Security Inc.")
> print("-- where security is our middle name\n")
>  
> password = input("Enter your password: ")
>  
> if password == "secret":
>     print("Access Granted")
>  
> input("\n\nPress the enter key to exit.")
>  
>  
> and the traceback error im getting
>  
> Traceback (most recent call last):
>   File "G:\Programming\Python\programming python\chapter3\password.py", line 7,
> in <module>
>     password = input("Enter your password: ")
>   File "<string>", line 1, in <module>
> NameError: name 'secret' is not defined


  Hi Shane,

Normally, a NameError would indicate a variable that's not defined. Here it's a it more tricky. You're probably using Python 2.x, while the code is for Python 3.x. There are several changes between Python versions 2 and 3 that make the two languages different (or rather, the interpreters handle some code differently). One difference how the function 'input()' is handled, and that difference is big enough for your code to crash. 
I would hope that the book somewhere in the beginning mentions the code is for Python 3 and not for Python 2. The best solution for you is to try and install Python 3 and use that to proceed through the code in the book.

  Evert

NB: if-else are not actually loops, but rather form a conditional statement. You can, then, use such a if-else construction to break out of a loop, which probably will be shown in some later code. Just to be pedantic ;-).


> I know this is probably very simple but for some reason i cannot get my head around it.
>  
> thank you for any help in advance
>  
> shane
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list