首先我们先讨论一下python2中的input()函数和raw_input()函数:
- . python2中的input():
- 首先,输入数字:
\>>> number = input("Please enter a number: ")
Please input a number: 18
\>>> a
18
- 然后我们**输入字符**:
\>>> name = (" Please enter your name: ")
Please enter your name: wwww
Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
name = input("Please enter your name: ")
File "<string>", line 1, in <module>
**NameError: name 'wwww' is not defined**
这里报错说,我们输入的名字没有定义,说明**没有以字符形式输入的直接当成变量处理**了,而**要 输入一个名字时我们要在名字两边加引号**。
\>>> name = (" Please enter your name: ")
Please enter your name: 'ningjing'
\>>> name
'ningjing'
然后我们再验证一下输入的名字没有加引号的时候是否真的当成变量处理
\>>> www ='ningjing'
\>>> name = input("Please enter your name: ")
Please enter your name: www
\>>> name
'ningjing'
验证结果正确
- .raw_input()也用同样的方式验证:
\>>> number = raw_input("Please enter a number: ")
Please enter a number: 666
\>>> number
'666'
\>>> name = raw_input("Please enter your name: ")
Please enter your name: ningjing
\>>> name
'ningjing'
这里不难看出raw_input不管你输入的是数字还是字符,系统都默认为字符格式。而input 要求用户需要知道要输入的是什么类型。
然后我们来讨论一下python3的input()函数:
\>>> number = raw_input("Please enter a number: ")
Please enter a number: 666
\>>> number
'666'
\>>> name = raw_input("Please enter your name: ")
Please enter your name: ningjing
\>>> name
'ningjing'
功能跟python2的raw_input()函数功能一样,python3进行优化,把python2的input()删除,将raw_input()改名为input()