问题
Total Newbie question for which I have searched the site. I am running a really simple program in Chapter 2 of Automate the Boring Stuff and I keep getting a NameError. The first line is
if name == 'Alice':
And it results in
NameError: name 'name' is not defined
Any thoughts on this. Cannot find this NameError in the index or any sites.
Thanks
回答1:
In the book you missed this comment above the code: "(Pretend name was assigned some value earlier.)". So you need to do that. For example (assuming Python 3):
name = input("Please enter your name: ")
if name == 'Alice':
print('Hi, Alice.')
By the way, next time you are searching for this kind of thing in a search engine, prefix the exception type with "python", for example "python NameError".
来源:https://stackoverflow.com/questions/39083806/newbie-inquiry-about-nameerror