python输入

In this tutorial we will learn about the most common function input() that we use frequently to take keyboard input from the user from the console. In our many tutorial we have used this, today we will see python input function more closely.

在本教程中,我们将学习最常用的函数input() ,我们经常使用它们从控制台获取用户的键盘输入。 在我们的许多教程中,我们都使用了它,今天,我们将更加紧密地了解python输入函数。

Python input() (Python input())

Python input function is present in the python builtins.py. It reads a string from standard input and the trailing newline is stripped.

Python输入函数位于python builtins.py 。 它从标准输入中读取一个字符串,并删除尾随的换行符。

When the input() statement is executed then the program get paused until user gives the input and hit the enter key.

当执行input()语句时,程序将暂停,直到用户提供输入并按下Enter键。

input() returns the string that is given as user input without the trailing newline.

input()返回作为用户输入给出的字符串,不带尾随换行符。

Python获取用户输入 (Python get user input)

Let’s have a look at a simple example to get user input using python input function.

让我们看一个使用python输入函数获取用户输入的简单示例。

# taking an input from the keyboard
a = input()# taking another input from the keyboard
b = input()c = a + b
print("a + b = %s + %s = %s" % ( a, b, c ))

This will output:

这将输出:

45
12
a + b = 45 + 12  = 4512

Oops! What is the output? Addition of 45 and 12 is 4512 ?? It’s because the input() method returns String that is given from the keyboard input. To do what we really wanted to, we have to type cast it to integer as following:

糟糕! 输出是什么? 45和12的加法是4512 ?? 这是因为input()方法返回从键盘输入中给出的String 。 要执行我们真正想要的操作,我们必须将其类型转换为整数,如下所示:

c = int(a) + int(b)

Now it will output:

现在它将输出:

45
12
a + b = 45 + 12 = 57

So, after taking input, cast it as the way you want it.

因此,在接受输入后,将其转换为所需的方式。

带有String消息的Python输入函数 (Python input function with String message)

In the above example we don’t get any hint what we should do. To give users about the instructions, we can take input as following:

在上面的示例中,我们没有任何提示我们应该做什么。 为了向用户提供这些说明,我们可以进行如下输入:

a = input('Please Enter the first number = ')
b = input('Enter the second number = ')
c = int(a) + int(b)
print("Summation of a + b = %s + %s = %s" % ( a, b, c ))

This will output:

这将输出:

Please Enter the first number = 45
Enter the second number = 12
Summation of a + b = 45 + 12 = 57

另一个简单的Python用户输入示例 (Another simple Python user input example)

The following example take the name of the user and finds out the number of occurrance of vowels in it.

下面的示例使用用户名,并找出其中的元音出现次数。

# taking input from prompt
name =input('what is your name? ')
print('Hello %s' % name)# taking a counter to count the vowels
count = 0
for i in name:i = i.capitalize()if i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U':count = count + 1print('Your name contains %s vowels' % count)

This will output:

这将输出:

Another thing that I should mention about the python input function is that it raises an error if the user hits EOF ( for *nix: Ctrl-D, Windows: Ctrl-Z+Return). The raised error is EOFError. In the above example if you press Ctrl-D then you will see the output as:

关于python input函数,我还应该提到的另一件事是,如果用户点击EOF(对于* nix:Ctrl-D,Windows:Ctrl-Z + Return),它将引发错误。 引发的错误是EOFError 。 在上面的示例中,如果您按Ctrl-D,则输出将显示为:

what is your name? ^D
Traceback (most recent call last):File "D:/T_Code/PythonPackage3/Input.py", line 2, in name =input('what is your name? ')
EOFError: EOF when reading a line

That’s all for a quick roundup on python input function and how to get user input in python.

这就是对python输入函数以及如何在python中获取用户输入的快速综述。

翻译自: https://www.journaldev.com/15815/python-input

python输入

python输入_Python输入相关推荐

  1. python字符串输入_Python输入字符串的方法和实例代码

    Python怎么输入字符串 首先,要显示字符串,直接 print(字符串) 就可以显示了. 字符串可以用单引号或者双引号,python中没有字符类型. 多个字符串直接连着写,就表示连接,字符串内部的转 ...

  2. python循环输入错误重新输入_python输入三次错误密码,用户锁定问题

    1.新建一个文件,用以存放白名单用户(正确注册的用户 格式:username:password),再建一个文件,用以存放黑名单用户(输入三次用户名均错误的用户). 2.读取白名单文件,将内容赋值给一个 ...

  3. python分行输入_python输入换行

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! abcn123n(其中n代表换行) print('abc') print ('1 ...

  4. python多行字符串输入_python中怎么输入多行字符串

    python中怎么输入多行字符串,疾风,不言,努力,人生,起风了 python中怎么输入多行字符串 易采站长站,站长之家为您整理了python中怎么输入多行字符串的相关内容. Python中输入多行字 ...

  5. python写界面输入测试脚本_python+Selenium自动化测试——输入,点击操作

    这是我的第一个真正意思上的自动化脚本. 1.练习的测试用例为: 打开百度首页,搜索"胡歌",然后检索列表,有无"胡歌的新浪微博"这个链接 2.在写脚本之前,需要 ...

  6. python输入程序_Python 程序设计中的输入与输出介绍

    关于Python 编程语言中的输入输出,其实我们在前两几节中已经接触过了.这节我们将具体的介绍一下Python中的输入与输出.什么是输入输出呢? 用户告诉计算机程序所需的信息,就是输入:程序运行结束告 ...

  7. python怎么将输入的数字变成列表_Python键盘输入转换为列表的实例

    Python输入字符串转列表是为了方便后续处理,这种操作在考试的时候比较多见. 1.在Python3.0以后,键盘输入使用input函数 eg1. >>> x=input >& ...

  8. python键盘输入数组_python 二维数组切割Python读取键盘输入的2种方法

    Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.如下: 1.raw_input 2.input raw_input函数raw_input() 函数从标准输入读取一个行,并返 ...

  9. python数字求和输入完第一个数没反应_Python 数字求和

    Python 数字求和 以下实例为通过用户输入两个数字,并计算连个数字之和: # -*- coding: UTF-8 -*- # Filename :test.py # author by : www ...

最新文章

  1. opencv_contrib编译失败解决方法
  2. android之android Studio 安装后打不开的解决方法
  3. iOS 获取手机信息
  4. java 字符 几个字节_java中字符串占几个字节
  5. 使用POI读写word docx文件
  6. C++ 常见错误(03) —— cout输出图像路径
  7. 学linux哪个版本号,初学Linux哪个发行版本好?这些更合适!
  8. 【ViPER音效插件】,完美提升电脑音乐播放效果
  9. 主机游戏神作和排行榜
  10. [Java]Axis需要高版本的J2sdk:j2sdk-1_4_2_08
  11. 计算机网络通信的媒体介质,计算机网络基础:常见的网络传输介质
  12. 保险精算--第8周作业
  13. ESP32开发路程——环境搭建、引脚、烧录、UART、ADC、WS2812、RFID、DAC、FreeRTOS、CJSON
  14. 开发软件的步骤是什么
  15. 一文深入理解 Kubernetes
  16. 计算机上的数学符号怎么打平方,平方米符号电脑上怎么打
  17. 高分一号影像处理流程
  18. 无线电波的波段、频率和应用
  19. Gradle Composite builds 扩展使用
  20. 第一章 甄士隐梦幻识通灵 贾雨村风尘怀闺秀

热门文章

  1. 编写手机脚本入门篇 2---bat 命令
  2. 一道经典面试逻辑题的python解法
  3. 正大国际期货:恒指的六种行情
  4. python 使用listdir 遍历目录
  5. 【转载】Shell判断字符串包含关系的几种方法
  6. 关于rospy等库文件倒入pycharm的办法
  7. 在EBS R12.0.6系统内查看用户的输出请求文件方法(不是自己提交的请求)
  8. 【PHP100例】PHP处理表单数据
  9. 服务器文件扫描,网站目录文件扫描工具dirbuster
  10. 经验正交函数分析法(EOF)在matlab上的实现