首页 > 解决方案 > 打印语句后面的输入语句首先打印

问题描述

我有一个打印语句 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

任何人都知道为什么会发生这种情况,这看起来像是两条语句之间的竞争,以访问打印功能并且输入似乎获胜,除非您延迟其执行。

标签: python

解决方案


与其拖延,不如试试

import sys

print('Warning this program renames files')
sys.stdout.flush()
char = input('Enter  Y to continue N to quit')

推荐阅读