python等待用户输入_Python等待时间,等待用户输入

python等待用户输入

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

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

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 time

print('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版本中出现。

翻译自: https://www.journaldev.com/23360/python-wait-time-wait-for-input

python等待用户输入

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值