首页  >  文章  >  后端开发  >  python:中input()与raw_input()的详解

python:中input()与raw_input()的详解

黄舟
黄舟原创
2017-10-07 11:40:331662浏览


实验

a = input('请输入:')
print a

如果输入字符串,则马上报错:

请输入:str  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>

但是如果输入整数,却不会报错:

请输入:1010

如果把 input 改成 raw_input ,则可以正常记录键盘输入的字符串:

a = raw_input(&#39;请输入:&#39;)print a
请输入:str
str

原因

原因就在于,input 只能接受整型输入:

a = input(&#39;请输入:&#39;)print type(a)
请输入:10<type &#39;int&#39;>

raw_input 可以接受字符串输入:

a = raw_input(&#39;请输入:&#39;)print type(a)
请输入:str
<type &#39;str&#39;>

以上是python:中input()与raw_input()的详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn