2

Searched a lot about this but it is not working in Python 3.7.3 Shell.

There a lot of similar question (for example Remove and Replace Printed items which refers to an 8 years old Python version though), where they succesfully solve the problem using control characters \r and \b. But in my case I'm not able to solve the problem.

I would like to have an output like amount: x/y (where x current position and y the length of a string or a list) which is updated at each iteration, ie the part x/y has to be deleted and the part x+1/y has to be written, while keeping the string amount:.

What I tried so far

(1)

import sys
import time
word='simple'
for i in range(len(word)):
    if i+1 < 10:
        print('amount: '+'0'+str(i+1)+'/'+str(len(word)+1), end='\r')
    else:
        print('amount: '+str(i+1)+'/'+str(len(word)+1), end='\r')
    time.sleep(0.5)

output

enter image description here

(2)

import sys
import time
word='simple'
for i in range(len(word)):
    if i+1 < 10:
        sys.stdout.write('amount: '+'0'+str(i+1)+'/'+str(len(word)))
    else:
        sys.stdout.write('amount: '+str(i+1)+'/'+str(len(word)))
    sys.stdout.write("\b \b")
    time.sleep(0.5)

output

enter image description here

sound wave
  • 3,191
  • 3
  • 11
  • 29
  • 3
    IDLE does not support control characters like '\r' and '\b' (see https://bugs.python.org/issue23220). Run in a shell (like Terminal on macOS and Linux). – 101arrowz May 23 '19 at 18:54
  • Possible duplicate of [Remove and Replace Printed items](https://stackoverflow.com/questions/5290994/remove-and-replace-printed-items) – Error - Syntactical Remorse May 23 '19 at 18:54

0 Answers0