gpt4 book ai didi

python - 当程序在 python 中运行时,如何打印到控制台?

转载 作者:太空狗 更新时间:2023-10-29 20:20:29 33 4
gpt4 key购买 nike

<分区>

我有一个正在运行的算法需要一段时间,所以我想通过打印到控制台来跟踪它的运行情况。

所以像这样:

import sys

def myMethod():
i = 0
while (i<1000000):
i = i+1
output_str = str(i) + "\n"
sys.stdout.write(output_str) # same as print
sys.stdout.flush()

myMethod()

我怎样才能在它运行时而不是在结束时打印它?

编辑,解决方案: - 发布修改后的代码。当您使用

在 linux 终端中运行此代码时,此代码工作正常
 python filename.py

但是当我在 Wing 101 IDE 中运行它时 - 通过按下绿色播放按钮(“在 python shell 中运行编辑器的内容”) - 它会等到程序完成后再输出。

Apparently it's not possible to flush stdout in the Wing IDE.

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com