python3中raw_input和input进行了整合,只有input函数

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>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值