zoukankan      html  css  js  c++  java
  • Py与Py3的区别之输入input()函数

    • Python 2.7中,一般是使用的input()比较常规些,可是也可以使用raw_input();他们仍有以下不同之处

    C:\Windows\system32>python
    Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
    Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.

    >>> s=input("请输入:")
    请输入:saa
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'saa' is not defined

      >>> s=input("请输入:")

      请输入:'asxs'

      >>> s
      'asxs'

      当我使用raw_input()函数时:

    >>> s=raw_input("input something:")
    input something:哈哈
    >>> s
    '\xb9\xfe\xb9\xfe'
    >>> s=raw_input("input something:")
    input something:axsx
    >>> s
    'axsx'

     >>> s=raw_input("input something:")
     input something:0
     >>> s
     '0'
      >>> type(s)
      <type 'str'>

      对于Python2.7 raw_input()函数,对于任何输入,raw_input()函数都会把它完全当做字符串来处理

      但是使用input()输入一些数字、列表、元组等类型数据是不会报错的

    >>> s=input("input something:")
    input something:1234556
    >>> s
    1234556
    >>> type(s)
    <type 'int'>
    >>> s=input("input something:")
    input something:(1,2,'a',999)
    >>> s
    (1, 2, 'a', 999)
    >>> type(s)
    <type 'tuple'>
    >>> s=input("input something:")
    input something:[1,2,[12,2],42,(2,2,'a')]
    >>> s
    [1, 2, [12, 2], 42, (2, 2, 'a')]
    >>> type(s)
    <type 'list'>

    • 然而在Python3中,甚至都没有raw_input()这个函数

      

      不过Python3支持input()函数的使用,但是,它又能直接接受一串字符:

    >>> s=input("input something:")
    input something:aaaaa
    >>> s
    'aaaaa'
    >>> s=input("input something:")
    input something:(12,23,'a')
    >>> s
    "(12,23,'a')"
    >>> type(s)
    <class 'str'>
    >>> s=input("input something:")
    input something:123
    >>> s
    '123'
    >>> type(s)
    <class 'str'>
    >>> s=input("input something:")
    input something:'asx'
    >>> s
    "'asx'"
    >>> s=input("input something:")
    input something:"wsacvd12324qaa"
    >>> s
    '"wsacvd12324qaa"'

      可以看到,对于任何输入,Python3的input()函数都会把它完全当做字符串来处理


      总结一波,Python2raw_input()函数和Python3的 input()函数的功能几乎等同,它对会把用户的输入当做一整个字符串的内容来处理,输出的类型也都是str字符串类型;

      Python3不支持 raw_input()函数;

      Python2input()简直是神一般的存在,十分智能化地识别用户输入的内容并给予相应的类型,单数输入字符串时候需要给字符串增添上 'xxx' "xxx" 引号。

      以上

  • 相关阅读:
    读写csv文件
    安卓跳转
    求时间精确到秒的数
    航空公司客户价值分析
    利用LM神经网络和决策树去分类
    拉格朗日插值法
    ID3
    K最近邻
    贝叶斯分类
    FilterDispatcher已被标注为过时解决办法
  • 原文地址:https://www.cnblogs.com/Higgerw/p/9858831.html
Copyright © 2011-2022 走看看