python - Python 没有按应有的方式运行某些代码行
问题描述
有人可以帮我处理这段代码吗?它的用途是进行 2 秒的定时输入,即使用户在控制台中输入或未输入某些内容(当时),它也需要打印最后一行。当我不输入任何内容时,在打印“太慢”后它会要求我输入相同的输入,所以我添加了 IF,但它并没有真正注意到它。我希望有人能帮助我。谢谢!
import time
from threading import Thread
i = 0
answer = None
def check():
time.sleep(2)
if answer != None:
return
print("Too Slow") #prints this if nothing is typed in (for 2 seconds)
if i == 0:
i = 1
Thread(target = check).start()
answer = input("Input something: ") #program doesn't even notice IF and asks me the second time for input
print("This should be printed instantly after printing Too Slow (when user doesn't input anything)")
解决方案
推荐阅读
- r - 根据周围的值替换 NA
- javascript - Ckeditor 4 uploadimage插件在上传后不更改Image Src
- c# - 如何将大于 1e-8 的浮点值强制为 0
- php - bodyTmpl magento 2 网格的 ui/grid/cells/html 是什么意思
- pyspark - 读取多个 CSV 文件的更好方法
- java - 如何使用 Selenium Java 自动化地图?或与页面坐标。请告诉我有没有其他方法可以自动化地图
- css-selectors - p:nth-child(n) 在 CSS div 中不起作用
- python - 创建一个包含文件地址作为键,内容作为值的python字典?
- json - JSON LD 显示我无法修复的错误
- knockout.js - 如何对包含 json 对象的可观察数组进行排序?