【问题标题】:How to append a variable and print it in place of the same line before it was?如何附加变量并将其打印到之前的同一行?
【发布时间】:2021-02-19 07:49:30
【问题描述】:

我想动态打印在一行上。

如果 x 条件匹配,我有一个列表,其元素将在另一个下方打印。 (设 x 为任意条件)

EG列表:

[1,2,4,5,6,20,11]

不管那个条件是真还是假,我想创建“x out of y elements”已经检查过的动态语句。

EG:考虑列表包含 100 个元素。

[20/100] 已检查。

[25/100] 已检查。

等等……

每当使用我的条件检查列表中的新元素时,无论条件是真还是假,这个数字“20”都应该在同一行上动态更新。

我搜索了很多,但没有找到这叫什么或如何实现它。请指导我。

提前感谢您的帮助!

【问题讨论】:

    标签: python loops dynamic


    【解决方案1】:

    您可以通过将print() 中的end 参数指定为'\r' 或Carriage Return 来做到这一点。

    例如:

    for i in range(100):
        # Your check goes here
        print('[{}/100] have been checked.'.format(i), end='\r')
    

    为了使更改更明显,您可以使用 time.sleep() 进行小间隔(例如 0.5 秒)。像这样的:

    import time
    for i in range(100):
        # Your check goes here
        print('[{}/100] have been checked.'.format(i), end='\r')
        time.sleep(0.5)
    

    【讨论】:

    • 非常感谢您的帮助!非常感谢!
    猜你喜欢
    • 2019-11-23
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2014-03-05
    相关资源
    最近更新 更多