Python输入错误 [英] Python input error
本文介绍了Python输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Mac OSX 10.9.5m上运行python 2.7.10,但无法正常工作.这是代码:
I'm running python 2.7.10 on Mac OSX 10.9.5m and it's not working. Here's the code:
# YourName.py
name = input("What is your name?\n")
print("Hi, ", name)
这是错误:
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
What is your name?
Ella
Traceback (most recent call last):
File "/Users/CentCom/Downloads/code/ch01/YourName.py", line 2, in <module>
name = input("What is your name?\n")
File "<string>", line 1, in <module>
NameError: name 'Ella' is not defined
>>>
推荐答案
在python 2.7.1中使用raw_input
:
use raw_input
in python 2.7.1:
name = raw_input("What is your name?\n")
否则,您必须依靠用户知道足够的内容来输入带引号的字符串.例如"David"
,或者输入尝试评估名称(变量/对象/等),如果范围内没有该名称,则会出现错误.
Otherwise you have to rely on the user to know well-enough to input in quoted string. like "David"
, or the input attempts to evaluate a name (variable/object/etc) and if there is no such name in scope, you'll get the error.
或者,使用异常处理:
name = input("What is your name?\n")
try:
print("Hi, ", name)
except NameError, e:
print("Please enclose your input with quotes")
这篇关于Python输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文