博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python等待用户输入_Python等待时间,等待用户输入
阅读量:2530 次
发布时间:2019-05-11

本文共 1661 字,大约阅读时间需要 5 分钟。

python等待用户输入

Sometimes we want our python program to wait for a specific time before executing the next steps. We can use function to pause our program for specified seconds.

有时我们希望python程序等待特定的时间才能执行下一步。 我们可以使用 函数将程序暂停指定的秒数。

Python等待时间 (Python wait time)

Let’s see a quick example where we will pause our program for 5 seconds before executing further statements.

让我们看一个简单的例子,在执行更多语句之前,我们将程序暂停5秒。

import timeprint('Hello There, next message will be printed after 5 seconds.')time.sleep(5)print('Sleep time is over.')

When we run this program, there will be 5 seconds delay between first print statement and second print statement.

当我们运行该程序时,第一个打印语句和第二个打印语句之间将有5秒钟的延迟。

Python等待用户输入 (Python wait for user input)

Sometimes we want to get some inputs from the user through the console. We can use input() function to achieve this. In this case, the program will wait indefinitely for the user input. Once the user provides the input data and presses the enter key, the program will start executing the next statements.

有时我们希望通过控制台从用户那里获得一些输入。 我们可以使用input()函数来实现这一点。 在这种情况下,程序将无限期等待用户输入。 用户提供输入数据并按Enter键后,程序将开始执行下一条语句。

sec = input('Let us wait for user input. Let me know how many seconds to sleep now.\n')print('Going to sleep for', sec, 'seconds.')time.sleep(int(sec))print('Enough of sleeping, I Quit!')

Below short screen capture shows the complete program execution.

简短的屏幕截图下方显示了完整的程序执行。

Surprising, there is no easy way to wait for user input with a timeout or default value when empty user input is provided. I hope these useful features come in future Python releases.

令人惊讶的是,当提供空用户输入时,没有简单的方法来等待带有超时或默认值的用户输入。 我希望这些有用的功能会在将来的Python版本中出现。

翻译自:

python等待用户输入

转载地址:http://namzd.baihongyu.com/

你可能感兴趣的文章
稀疏表示、字典学习和压缩感知(基本概念)
查看>>
新手小白Linux(Centos6.5)部署java web项目(mysql5.7安装及相关操作)
查看>>
ruby实现生产者和消费者
查看>>
node.js 之 http 架设
查看>>
MongoDB 备份与还原
查看>>
Oracle启动与关闭数据库实例
查看>>
Spring day01
查看>>
hihocoder-1740-替换函数
查看>>
Codeforce Round #219 Div2
查看>>
option value的值可以有空格 再试试吧
查看>>
.htaccess to httpd.conf
查看>>
node.js 基础学习笔记2
查看>>
hadoop中常见元素的解释
查看>>
BZOJ-1497 最大获利
查看>>
4-4 修改文件
查看>>
并发编程(十):AQS
查看>>
条件注释判断浏览器版本<!--[if lt IE 9]>
查看>>
Comparison among several SGD derivation
查看>>
ModelAndView同时向页面传递多个参数
查看>>
samba 配置参数详解
查看>>