7.Python_5: Input

作者: MissLady的学习志 | 来源:发表于2020-07-15 10:49 被阅读0次

Python_5: Input

****学 习 地 址 :****

计算机科学圈| 01000011 01010011 01000011

https://cscircles.cemc.uwaterloo.ca/5-input/

1. Input( ) Function

input()函数用于接收用户或程序的输入。

在python的shell中输入help(input),在交互式shell中输出为:

Help on built-in function input in module builtins:
input(prompt=None, /)
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

即,input()是内置函数,语法为input(prompt),prompt一个字符串,表示输入前的提示消息,默认为空。input()调用一次,对应用户一行输入,多余行不读,并且始终返回str。

注:input()调用一次,对应用户一行输入,多余行不读,并且始终返回str。

2.input函数在Python中运行

input( ) 函数在Python中的工作方式:

  • 当input()函数执行时,程序流程将停止,直到用户给出输入为止。

  • 当input()函数执行时,程序流程将停止,用户没有给出输入,运行会报错误“EOFError: EOF when reading a line”,见下图。

  • 输出屏幕上要求用户输入输入值的文本或消息(prompt内容)是可选的,即屏幕上的输入提示是可选的,默认为空。

  • 无论输入什么,input()函数都会在输入的值的左右两边加上引号,将其转换为字符串。

  • 在Python中,通过input()输入的用户输入始终为String格式,如果需要任何其他格式,则需要类型转换,见下图。

注:EOFError,字母缩写EOF表示 End Of File。从字面上说,这个提示消息意味着程序调用了input(),但是没有任何可用的输入可供读取。

注:通过input()输入的用户输入始终为String格式,如果需要任何其他格式,则需要类型转换。

3. 获取多个输入

使用split()方法

  1. 使用split()方法

    语法:

    input().split(separator,maxsplit)

    separator-分隔符(未提供,则任何空格都是)

    maxsplit-数字,表示字符串分割的最大次数(未提供,则没有限制)

    例:

  2. 使用List comprehension

3. Exercise & Answer

Coding Exercise:Echo

Write a program that reads one line of input, and prints out that same line two times.

my Answer

 a=input()
 print(a)
 print(a)</pre>

参考:

[1]https://cscircles.cemc.uwaterloo.ca/

[2]https://www.geeksforgeeks.org/taking-multiple-inputs-from-user-in-python/?ref=lbp

[3]https://www.w3schools.com/python/python_datatypes.asp

相关文章

网友评论

    本文标题:7.Python_5: Input

    本文链接:https://www.haomeiwen.com/subject/aoxqhktx.html