python-3.x - 如何结束包含 time.sleep 的 while 循环?
问题描述
假设我有这样的代码:
import time
try:
while True:
print("Hello World")
time.sleep(10)
except:
print("Ctrl+z was pressed") #Doesn't get executed
当我尝试在 python 3 中执行此代码时,我的 except 块中的内容不会执行。我能做些什么?
解决方案
您将始终卡在 while 循环中,因为条件始终为 True。您将永远不会退出 try 条件,因此永远不会执行 except 块。
如果我是正确的,crtl+z 只会让程序休眠,所以没有像 crtl+c 这样的终止信号,它会打破循环并让 except 块执行。
推荐阅读
- camunda - 在 Camunda 中,两个选项中优化编码的正确方法是什么?
- asp.net-mvc - 为什么 Html.DisplayFor 显示模型属性名称而不是属性值?
- python - Django Form:仅显示来自登录用户的多对象
- google-app-engine - AppEngine:搜索 API 的 Python 3 版本
- javascript - Javascript:在 Promise 完成后加载到 Promise 中的数据不可用
- angular - 角度将数字转换为波斯语
- scala - 处理protobuf文件scala中的下划线
- java - EOSIO-java Maven 依赖问题
- javascript - 如何循环数组并仅获取特定索引的值
- c# - XAML UI 使用 Observable 集合 MVVM 很慢