CubeEscpython

7.Python_5: Input

2020-07-15  本文已影响0人  MissLady的学习志

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中的工作方式:

注: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

上一篇 下一篇

猜你喜欢

热点阅读