NameError 新手查询 [英] Newbie inquiry about NameError

查看:67
本文介绍了NameError 新手查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上搜索过的所有新手问题.我在 Automate the Boring Stuff 的第 2 章中运行了一个非常简单的程序,但我不断收到 NameError.第一行是

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':

结果是

NameError: name 'name' is not defined

对此有什么想法吗?

推荐答案

book 中,您错过了此评论在代码上方:(假装名称之前已分配了一些值.)".所以你需要这样做.例如(假设 Python 3):

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.')

顺便说一句,下次您在搜索引擎中搜索此类内容时,请在异常类型前加上python",例如python NameError".

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".

这篇关于NameError 新手查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆