简体   繁体   English

Vscode 没有缓冲 python 打印

[英]Vscode not buffering python print

For example if I run this code in integrated vscode terminal:例如,如果我在集成的 vscode 终端中运行此代码:

for i in range(2):
    a = input()
    print(a)

And I paste the following:我粘贴以下内容:

a
b

The console will display:控制台将显示:

a
a
b
b

I want the console to display (without changing the code):我希望控制台显示(不更改代码):

a
b
a
b

Note: above is the typical behaviour when running python script from linux terminal or IDLE, but not in vscode.注意:以上是从 linux 终端或 IDLE 运行 python 脚本时的典型行为,但不是在 vscode 中。 I believe the issue is with the vscode console not buffering the prints.我认为问题在于 vscode 控制台没有缓冲打印。

If you want that kind of output you can use list comprehension to store the two letters in a list.如果您想要那种 output 您可以使用列表推导将两个字母存储在列表中。 Right now a and b are shown.现在显示ab So if we print the list, we will get the same pattern.因此,如果我们打印列表,我们将得到相同的模式。

inputs = [input() for i in range(2)]
print('\n'.join(inputs))

output output

a
b
a
b
myLetters=[]
for i in range(2):
    a = input()
    myLetters.append(a)
print(myLetters[0] + "\n"+  myLetters[1])

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

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