天天看点

Python100天27:python输入 input

作者:洪观群众不着调程序员

上文 中我们讲解了什么是标准的输入输出设备,当然现在的计算机的输入输出设备比过去的各类与表现形式多了很多。

Python100天26:python输入与输出input与print

Python100天27:python输入 input

当我们重新审视Python中的函数 input()时,才能有联想,这是输入。

Python input() function is used to take user input. By default, it returns the user input in form of a string.

Python input() Function Syntax

Syntax: input(prompt)

prompt [optional]: any string value to display as input message

Returns: Return a string value as input by the user.

看一下input函数的定义

  • input() function is used to take user input 获取用户的输入
  • By default, it returns the user input in form of a string 函数的返回值默认是string字符串
Read a string from standard input.  The trailing newline is stripped.
  
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.
             

可以进一步的分析这个函数的作用

  • Read a string from standard input 从标准的输入设备读取一个字符串
  • The trailing newline is stripped 尾部的换行符号被忽略。

什么是标准的输入呢

  • 在任意系统下,读取键盘输入,向屏幕输出信息.标准输入,标准输出
  • 这一点大家可以记住,当然这些输入,输入理论上是可以重写定向的,但是不在我们讨论的范围内容。

    回头看一个简单的input函数例子

    color = input("What color is rose?: ")
    print(color)           
    • "What color is rose?: " 是一段提示文字
    • color 变量是返回值,默认是字符串,因此即使用户输入的是数字,其实在Python层面这个也是一个字符串。本质上所以用户从键盘上输入的内容其实是还在什么数字的。在函数层面其实是字符串。如果要将字符串当数字使用,则需要进行数据类型转换
    • colorInt = int (color ) 使用i函数将输入的内容变成数字类型
    Python100天27:python输入 input
    color = input("What color is rose?: ")
    print(color)
    print(type(color))           
    Python100天27:python输入 input

    这里我们看到我们所输入的123在python层面 其实是一个字符串

    如何将键盘输入的 数字 “123”,转换成程序中可以运行的数字类型呢?

    color = input("What color is rose?: ")
    print(color)
    print(type(color))
    print()
    
    colorInt = int(color)
    print(colorInt)
    print(type(colorInt))           
    Python100天27:python输入 input

    使用int函数进行转换。

    color = input("What color is rose?: ")
    print(color)
    print(type(color))
    print()
    colorInt = int(color)
    print(colorInt)
    
    print(type(colorInt))
    print(10 -colorInt)
    print(10 -color)           
    Python100天27:python输入 input
    print(10 -colorInt)   # -113
    print(10 -color)           

    print(10 -color)f直接报错了。为什么呢?

    Traceback (most recent call last):
    File "D:\zsoft\project\eclipse_python\spider_newspaper_1\stepbystep100\048-color.py", line 17, in <module>
    print(10 -color)
    TypeError: unsupported operand type(s) for -: 'int' and 'str'           

    unsupported operand type(s) for -: 'int' and 'str'

    10 是数字,但是 color是一个字符串,这两个是不支持 减法运算的。

    -<------- 附录键盘的读取过程 ---------->

    题外话,键盘的读取本质 上也是操作系统Windos\macOS\Linux等与硬件密切配合的一个软件硬件的程序,一个小小的键盘输入其实背后蕴含了巨大复杂的程序交互。

    Python100天27:python输入 input
    • 键盘按下一个按键后,对应触发键盘中断处理程序
    • 通过扫描码得到对应的ascii码,然后将ascii码加入read_q队列中去
    • do_tty_interrupt函数负责读取read_q队列
    • 判断是否需要显示当前字符到屏幕上去,如果需要,则从read_q队列读取对应的字符,加入write_q队列中,然后调用tty_write输出到屏幕
    • 对字符进行一些处理后,加入tty->secondary队列中去(具体是什么处理,感兴趣可以自行查询资料)
    Python100天27:python输入 input
    Python100天27:python输入 input