简体   繁体   English

打印语句后的输入语句先打印

[英]Input statement following a print statement prints first

I have a print statement preeceeding and input statement. 我有一个打印语句的开头和输入语句。 However the input statement prints first. 但是,输入语句将首先打印。

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

The terminal screen the results are: 终端屏幕的结果是:

Enter Y to continue N to quit
Warning this program renames files

if I introduce some form of delay between the two statements it works correctly. 如果我在两个语句之间引入某种形式的延迟,则它可以正常工作。 Example code: 示例代码:

print('Warning this program renames files')
for i in range (1,10000):
    j=i*i
char=input('Enter  Y to continue N to quit')

the output in the terminal is printed in the correct order 终端中的输出以正确的顺序打印

Warning this program renames files
Enter  Y to continue N to quit

Anyone know why this happens it looks like a race between the two statement to access the print functions and input seems to win unless you delay its execution. 任何人都知道为什么会发生这种情况,除非您延迟执行,否则看起来这两个语句之间似乎在争用访问打印功能和输入的竞争。

Rather than a delay, try 不要拖延,而是尝试

import sys

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM