Board logo

标题: Python内置函数 - input() 函数 [打印本页]

作者: 龙听    时间: 2024-3-6 15:28     标题: Python内置函数 - input() 函数

Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型。

Python2.x 中 input() 相等于 eval(raw_input(prompt)) ,用来获取控制台的输入。

raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。

注意:input() 和 raw_input() 这两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。

除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。

注意:python3 里 input() 默认接收到的是 str 类型。

函数语法
  1. input([prompt])
复制代码
参数说明:

prompt: 提示信息

实例

Python2.x: input() 需要输入 python 表达式
  1. >>>a = input("input:")
  2. input:123                  # 输入整数
  3. >>> type(a)
  4. <type 'int'>               # 整型
  5. >>> a = input("input:")   
  6. input:"runoob"           # 正确,字符串表达式
  7. >>> type(a)
  8. <type 'str'>             # 字符串
  9. >>> a = input("input:")
  10. input:runoob               # 报错,不是表达式
  11. Traceback (most recent call last):
  12.   File "<stdin>", line 1, in <module>
  13.   File "<string>", line 1, in <module>
  14. NameError: name 'runoob' is not defined
  15. <type 'str'>
复制代码
Python2.x: raw_input() 将所有输入作为字符串看待
  1. >>>a = raw_input("input:")
  2. input:123
  3. >>> type(a)
  4. <type 'str'>              # 字符串
  5. >>> a = raw_input("input:")
  6. input:runoob
  7. >>> type(a)
  8. <type 'str'>              # 字符串
  9. >>>
复制代码

作者: 龙听    时间: 2024-3-6 15:31

Python3 input() 函数

Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型。

注意:在 Python3.x 中 raw_input() 和 input() 进行了整合,去除了 raw_input( ),仅保留了input( )函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型。

函数语法
  1. input([prompt])
复制代码
参数说明:

prompt: 提示信息

实例

input() 需要输入 python 表达式
  1. >>>a = input("input:")
  2. input:123                  # 输入整数
  3. >>> type(a)
  4. <class 'str'>              # 字符串
  5. >>> a = input("input:")   
  6. input:runoob              # 正确,字符串表达式
  7. >>> type(a)
  8. <class 'str'>             # 字符串
复制代码
input() 接收多个值
实例
  1. #!/usr/bin/python
  2. #输入三角形的三边长
  3. a,b,c = (input("请输入三角形三边的长:").split())
  4. a= int(a)
  5. b= int(b)
  6. c= int(c)

  7. #计算三角形的半周长p

  8. p=(a+b+c)/2

  9. #计算三角形的面积s

  10. s=(p*(p-a)*(p-b)*(p-c))**0.5

  11. #输出三角形的面积

  12. print("三角形面积为:",format(s,'.2f'))
复制代码
以上实例执行输出结果为:
  1. 请输入三角形三边的长:3 4 5
  2. 三角形面积为: 6.00
复制代码

作者: 龙听    时间: 2024-3-6 15:33

Python2.x 和 Python3.x 中 raw_input( ) 和 input( ) 区别

1、在 Python2.x 中 raw_input( ) 和 input( ),两个函数都存在,其中区别为:

raw_input( ) 将所有输入作为字符串看待,返回字符串类型。
input( ) 只能接收"数字"的输入,在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。

2、在 Python3.x 中 raw_input( ) 和 input( ) 进行了整合,去除了 raw_input( ),仅保留了 input( ) 函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型。

例如:
  1. Python 2.3.4 (#1, Feb  2 2005, 11:44:13)
  2. [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> user=raw_input("please input:")      
  5. please input:wei                          #  raw_input 输入  字符串  成功
  6. >>> user
  7. 'wei'
  8. >>> user=input("please input:")         
  9. please input:123                          #  input 输入  数字  成功(返回的是数字)
  10. >>> user
  11. 123
  12. >>> user=raw_input("please input:")
  13. please input:111               #  raw_input 输入  数字  成功(返回的还是当成字符串)
  14. >>> user
  15. '111'
  16. >>> user=input("please input:")
  17. please input:wei                          #  input  输入字符串   失败
  18. Traceback (most recent call last):
  19.   File "<stdin>", line 1, in ?
  20.   File "<string>", line 0, in ?
  21. NameError: name 'wei' is not defined
复制代码
在 Python 3.2.3 中 input 和 raw_input 整合了,没有了 raw_input:
  1. Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win
  2. 32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> user=raw_input("please input:")                 #没有了raw_input
  5. Traceback (most recent call last):
  6.   File "<stdin>", line 1, in <module>
  7. NameError: name 'raw_input' is not defined
  8. >>> user=input("please input:")
  9. please input:wei
  10. >>> user
  11. 'wei'
  12. >>> user=input("please input:")                     #input的输出结果都是作为字符串
  13. please input:123
  14. >>> user
  15. '123'
复制代码





欢迎光临 龙听期货论坛 (http://www.qhlt.cn/) Powered by Discuz! 7.2