python2中有input()函数和raw_input()函数
python3中只有input()函数
在python3中:
一:input函数默认结果是字符串,无论你输入的是整数/小数,都会默认加上引号,变成字符串
>>> a=input()
123
>>> type(a)
<class 'str'>
二:要得到一个整数,只需要使用强制类型转换就可以了
>>> a=int(input())
123
>>> type(a)
<class 'int'>
在python2中:
一、Python2.x中raw_input( )和input( )函数
在Python2.x中raw_input( )和input( ),两个函数都存在,其中区别为:
<span style="color:#000000"><code class="language-python"><span style="color:#006666">>>> </span>help(raw_input)
Help on built-<span style="color:#000088">in</span> function raw_input <span style="color:#000088">in</span> module __builtin__:
raw_input(...)
raw_input([prompt]) -> string
Read a string <span style="color:#000088">from</span> standard input. The trailing newline <span style="color:#000088">is</span> stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), <span style="color:#000088">raise</span> EOFError.
On Unix, GNU readline <span style="color:#000088">is</span> used <span style="color:#000088">if</span> enabled. The prompt string, <span style="color:#000088">if</span> given,
<span style="color:#000088">is</span> printed without a trailing newline before reading.</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
raw_input( )—将所有输入作为字符串看待,返回字符串类型
<span style="color:#000000"><code class="language-python"><span style="color:#006666">>>> </span>help(input)
Help on built-<span style="color:#000088">in</span> function input <span style="color:#000088">in</span> module __builtin__:
input(...)
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
input( )—–只能接收“数字”的输入,在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )
example:
<span style="color:#000000"><code class="language-python"><span style="color:#006666">>>> </span>user=raw_input(<span style="color:#009900">"please input:"</span>)
please input:wei <span style="color:#880000"># raw_input输入 字符串 成功 </span>
<span style="color:#006666">>>> </span>user
<span style="color:#009900">'wei'</span>
<span style="color:#006666">>>> </span>user=input(<span style="color:#009900">"please input:"</span>)
please input:<span style="color:#006666">123</span> <span style="color:#880000"># input 输入 数字 成功(返回的是数字) </span>
<span style="color:#006666">>>> </span>user
<span style="color:#006666">123</span>
<span style="color:#006666">>>> </span>user=raw_input(<span style="color:#009900">"please input:"</span>)
please input:<span style="color:#006666">111</span> <span style="color:#880000"># raw_input 输入 数字 成功(返回的还是当成字符串) </span>
<span style="color:#006666">>>> </span>user
<span style="color:#009900">'111'</span>
<span style="color:#006666">>>> </span>user=input(<span style="color:#009900">"please input:"</span>)
please input:wei <span style="color:#880000"># input 输入字符串 失败 </span>
Traceback (most recent call last):
File <span style="color:#009900">"<stdin>"</span>, line <span style="color:#006666">1</span>, <span style="color:#000088">in</span> ?
File <span style="color:#009900">"<string>"</span>, line <span style="color:#006666">0</span>, <span style="color:#000088">in</span> ?
NameError: name <span style="color:#009900">'wei'</span> <span style="color:#000088">is</span> <span style="color:#000088">not</span> defined </code></span>