[英]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: 有三个错误:
p of print needs to be lowercase and 打印的p必须为小写,
a closing parenthesis was missing 缺少右括号
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.