Carriage return not working in a particular case

29 Views Asked by At

I want to overwrite the output of my programme so that each line overwrites the previous one. I know that I can use carriage return for this. This is where things get interesting.

This is working fine:

for x in range(10):
    print(x, end='\r')
    time.sleep(0.1)

However, this, which is what I need to do, is not wrking:

def ft_progress(list):
    to_print = range(0, len(list), min(int(len(list) / 30) + 1, 30))
    for i2 in list:
        i = abs(i2)
        print("[", str("%.2f" % (100 * (i + 1) / len(list))).rjust(6), "% ]", end='\r')
        yield i


for elem in ft_progress(X):
    ret += (elem + 3) % 5
    time.sleep(0.01)
    print()
    print(ret)

I need the yield to return a value as well, that I will need to print. In this last case, the output is not overwritten. Why is this happening? It is driving me nuts. I have already tried things like starting the print statement with '\r', but it is not working.

0

There are 0 best solutions below