Python输入法标签在打印时出错

Python输入法标签在打印时出错,python,Python,我正在自己练习python,并努力学习输入法。我的程序如下 a = (input('Enter any alphabet: ')) print 'type is: ',a if a=='a': print 'The given character is vowel a ' elif a=='e': print 'The given character is vowel e' elif a=='i': print 'The given character is vowel

我正在自己练习python,并努力学习输入法。我的程序如下

a = (input('Enter any alphabet: '))
print 'type is: ',a
if a=='a':
    print 'The given character is vowel a '
elif a=='e':
    print 'The given character is vowel e'
elif a=='i':
    print 'The given character is vowel i'
elif a=='o':
    print 'The given character is vowel o'
elif a=='u':
    print 'The given character is vowel u'
else:
   print 'The give character is a consonent'
print "Thats all folks"
当我输入如下所示的一位数字母表时,它给出了一个错误

Enter any alphabet: a
Traceback (most recent call last):
File "Demo_if_ladder.py", line 1, in <module>
a = input('Enter any alphabet: ')
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
输入任意字母表:a
回溯(最近一次呼叫最后一次):
文件“Demo_if_ladder.py”,第1行,在
a=输入('输入任何字母:')
文件“”,第1行,在
NameError:未定义名称“a”

为什么会发生这种情况,如果我在单引号中输入一位字母,程序运行良好,但它不接受没有单引号的一位字母,它看起来像是从print语句语法运行python 2。 在Python2中,
input(…)
尝试像运行命令一样运行内容。因此,当您在中输入某些内容时,python将尝试执行它


问题的解决方案是使用
原始输入(…)
,它将返回一个字符串。

您使用的是Python 2

在Python2中,input接受Python命令。您可以使用原始输入来获得所需的效果

单引号解决了这个问题,因为Python命令中的文本是用单引号编写的

如果运行以下命令,您将清楚地了解:

a = (input('please, write: "list(range(10))"\n'))
print a
a = raw_input('please, write: "list(range(10))"\n')
print a
在Python3中,输入的工作方式与您预期的一样,但print语句已更改为函数。在Python3中,您应该更改

print a


您应该使用
原始输入
。顺便说一句,现在是您切换到Python3.xversion.No的时候了。您应该使用Python3。我被告知从Python2.x开始,因为初学者Python2正在逐步淘汰,而且有一股强大的力量推动着将所有内容都转移到Python3中。我建议从Python3开始(并一直坚持下去)。
print(a)