python - 打印语句后面的输入语句首先打印
问题描述
我有一个打印语句 preeceeding 和输入语句。但是,输入语句首先打印。
print('Warning this program renames files')
char =input ('Enter Y to continue N to quit')
终端屏幕的结果是:
Enter Y to continue N to quit
Warning this program renames files
如果我在两个语句之间引入某种形式的延迟,它会正常工作。示例代码:
print('Warning this program renames files')
for i in range (1,10000):
j=i*i
char=input('Enter Y to continue N to quit')
终端中的输出以正确的顺序打印
Warning this program renames files
Enter Y to continue N to quit
任何人都知道为什么会发生这种情况,这看起来像是两条语句之间的竞争,以访问打印功能并且输入似乎获胜,除非您延迟其执行。
解决方案
与其拖延,不如试试
import sys
print('Warning this program renames files')
sys.stdout.flush()
char = input('Enter Y to continue N to quit')
推荐阅读
- html - 保存到主屏幕的 Safari 网页卡在独立模式下
- java - 尝试连接 Finch 机器人时 Mac 上的 Eclipse 错误:持久 UI 无法打开文件
- bash - 如何在一个文件夹中的文件夹内移动文件
- javascript - 如何通过单击导航按钮开始/结束smoke.js 动画?
- nuget - 如何向我的 dotnet nuget 包添加自定义许可证?
- apache-spark - 从 Spark SQL 与 RDD api 写入数据
- c# - C# - 使用泛型使用派生类型覆盖接口的返回类型
- vue.js - 如何在 VueJS 单页应用程序中添加 Matomo 跟踪代码?
- java - 如何创建一个二维数组,从中心以螺旋形填充?出现错误:线程中的异常...-1 和 9
- r - 更改 flexdashboard 中使用的 shiny::renderText() 内的行