简体   繁体   English

运行此代码并输入信息时,为什么会出现“名称错误:名称”…“未定义”?

[英]Why do I get a “name error: name ”…“ is not defined ” when I run this code and enter information?

When I run this simple script and enter information an error appears which says: Name error: name'...' is not defined . 当我运行此简单脚本并输入信息时,出现错误,提示: Name error: name'...' is not defined

Name = str(input("Enter the name of your friend: "))
Age = int(input("Enter age of that friend: "))
print("The name of your friend is {} and their age is {}".format(Name, Age))

I have modified your code to the correct state: 我已将您的代码修改为正确的状态:

name = str(input("Enter the name of your friend"))
age = int(input("Enter age of that friend"))
print("The name of your friend is {} and their age is {}".format(name, age))

There were three errors: 有三个错误:

  1. p of print needs to be lowercase and 打印的p必须为小写,

  2. a closing parenthesis was missing 缺少右括号

  3. f of format also needs to be in lowercase. 格式的f也必须小写。

To expand on Tarun Guptas answer, the convention in python is that function names always start with lowercase letters. 为了扩展Tarun Guptas答案,python中的约定是函数名称始终以小写字母开头。 Uppercase first letters are reserved for classes. 大写的首字母保留给类。 All the inbuilt functionality of python sticks to these conventions. python的所有内置功能都遵循这些约定。 The same is true for variable names, never start them with uppercase letters. 变量名称也是如此,不要以大写字母开头。

If you find yourself hunting a lot after syntax errors, get yourself an editor with a good python linter, that is, inbuilt functionality that checks the code on the fly. 如果您发现自己在语法错误后经常遇到麻烦,请为自己准备一个具有良好python linter的编辑器,即内置功能可以即时检查代码。 This will help you make quick progress in avoiding the most common mistakes. 这将帮助您快速避免出现最常见的错误。

As you advance in python, you should really check out the python style guide . 随着python的发展,您应该真正查看python样式指南 Most of the conventions in python are not strictly mandated by the language, however if you ever plan to interact with other programmers, they will hate you with a passion you if you don't stick to pep8. python中的大多数约定不是该语言严格要求的,但是,如果您打算与其他程序员进行交互,那么如果您不坚持使用pep8,它们会以您的热情来恨您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在另一个函数上定义它时,为什么我得到名称未定义的错误? - Why do i get Name is not defined error for python when i defined it on another function? 为什么我认为是“未定义名称‘play’”却出现错误? - Why do I get an error "name 'play' is not defined" when I think it is? 为什么会出现错误“ NameError:未定义名称'kh'” - Why do I get the error “NameError: name 'kh' is not defined” 为什么会出现错误(NameError名称“ first”未定义 - Why do I get and Error (NameError name 'first' not defined 为什么会出现错误:“ NameError:未定义全局名称'pupiluserinputbox'” - Why do I get the error: “NameError: global name 'pupiluserinputbox' is not defined” 为什么在类中调用另一个函数时出现“未定义名称”错误消息? - Why do I get a “Name is not defined” error message when calling another function within a class? 为什么我不断收到名称错误:名称“模型”未定义 - Why do I keep getting Name Error: name 'model' is not defined 为什么在定义变量时出现名称错误? - Why am I getting a name error when I defined the variable? 为什么我得到错误没有这样的列名 - Why do I get the error no such column name 当我尝试运行 Odoo 时,为什么会出现错误“名称节点不能与 'None' 常量一起使用”? - Why do I get the error “Name node can't be used with 'None' constant” when I try to run Odoo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM