Skip to main content
 首页 » 编程设计

python之更新打印数据调试python

2025年05月04日14Renyi-Fan

我有一个 python 程序,它有一些随时间变化的变量。我想将它们打印到屏幕上,而不是换行打印。例如:

for i in range(1,10): 
    v1 = i 
    v2 = i+1 
    v3 = i+2 

当程序运行时,我希望显示屏显示:

variable 1 value = v1  #v1 changes over time 
variable 2 value = v2  #v2 changes over time 
variable 3 value = v3  #v3 changes over time 

通过覆盖 v1 v2 和 v3 的先前打印值

可以吗?我知道可以使用 '/r', 打印一行来完成,但是这次我想打印更多行...

请您参考如下方法:

您可以根据 this other answer 执行以下操作(但这可能不适用于所有显示器) :

import os 
for i in range(1,10): 
    # First clear the screen 
    os.system('cls' if os.name == 'nt' else 'clear') 
    print('variable 1 value = %s' % i) 
    print('variable 2 value = %s' % i+1)         
    print('variable 3 value = %s' % i+2)