[Tutor] Getting confusing NameError
Hi Tutors 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 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting confusing NameError
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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting confusing NameError
use raw_input instead of input . as using input is unsafe as it evaluates your input as python command. for e,g. x = input(enter command) and u entered 1 + 2 as input., then it will evaluate 1+2 = 3 = x. -nitin On Wed, Aug 18, 2010 at 12:45 PM, shane brennan wiserwaylan...@gmail.comwrote: Hi Tutors 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 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting confusing NameError
On 18/08/2010 09:15, shane brennan wrote: Hi Tutors 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 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor input() [1] expects a valid Python expression as input as it gets evaluated and is not technically safe. raw_input() [2] is what you should be using instead. [1] http://docs.python.org/library/functions.html#input [2] http://docs.python.org/library/functions.html#raw_input Hope that helps. Welcome to the list and enjoy your stay. -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting confusing NameError
shane brennan wiserwaylan...@gmail.com wrote 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. Evert has already pointed out that the code is for Python v3 and it looks like you are using Python v2. If your other book is based on v2 then using the two side by side will be confusing. If both are on v3 then you need to upgrade your Python to v3. If they are mixed versions then you need to decide on only one book and follow whichever version it uses. You can find both v2 and v3 versions of my tutorial on my web site. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting confusing NameError
Hi all Thank you for all the help. I had upgraded the machine to v3 but for some reason 2 hadnt removed properly, once i did some digigng i found the redundant entries in the registry and the libraries that didnt remove and cleared them up . script is running fine now:) thanks for all the help Espcially thanks for the pointers and information on the difference between raw_input and input. again thanks for everything, and hopefully ill be around here for a long time and once i get a more experienced i can contribute back On Wed, Aug 18, 2010 at 09:37, Alan Gauld alan.ga...@btinternet.com wrote: shane brennan wiserwaylan...@gmail.com wrote 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. Evert has already pointed out that the code is for Python v3 and it looks like you are using Python v2. If your other book is based on v2 then using the two side by side will be confusing. If both are on v3 then you need to upgrade your Python to v3. If they are mixed versions then you need to decide on only one book and follow whichever version it uses. You can find both v2 and v3 versions of my tutorial on my web site. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting confusing NameError
On Wed, Aug 18, 2010 at 3:48 AM, shane brennan wiserwaylan...@gmail.comwrote: snip again thanks for everything, and hopefully ill be around here for a long time and once i get a more experienced i can contribute back You don't need to be /too/ experienced. Contributing here is actually how I learned Python as much and as well as I did. When someone asked a question (especially interesting ones), I made it a point to try and answer them. If I didn't already know the answer I would go Google and try to find the answer. Sometimes it was over my head and I wasn't able to form a decent reply before others, but many times I was able to understand the problem and question and fire off some fairly good information. And if you preface your post with I'm new to this part of Python, but I looked it up and this is what I found... or some other self effacing statement that shows you're pretty confident in your answer but you're perfectly willing to be taught if you were wrong - which will (usually) keep you from looking like a cocky noob, and look like more like a humble acolyte trying to do your best to help out a fellow Pythonista. At least that's my experience ;) -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor