0

I'm trying to create a loading text that alternates between "Loading", "Loading.", "Loading..", "Loading...", and "Loading...." while updating the previous entry. I found similar situations online that use either sys.stdout or a \r end to the print function but have had no luck with either

The code i'm using is:

import time 
tick = 0
while True:
    print("Loading" + "." * tick, end="\r")
    if tick < 4:
        tick += 1
        time.sleep(0.4)
    else:
        tick = 0

The output I'm getting is:

LoadingLoading.Loading..Loading...Loading....LoadingLoading.Loading..Loading...

until I force quit the execution. i haven't seen the problem mentioned in other threads but if anyone has any idea what might be causing the issue I'd be VERY grateful.

  • Where are you running the code? If using IDLE for example this won't work. But if you run your script regularly from `cmd` it should work fine. But even then you will have another problem. This does not **clear** the screen, only moves the caret back. So after the print of 4 dots you will no longer see any change. You might want to pad with spaces (to emulate deletion) – Tomerikoo Feb 19 '20 at 21:25
  • https://stackoverflow.com/a/35985184/6045800 – Tomerikoo Feb 19 '20 at 21:36

1 Answers1

0

Depending on your platform you need both a carriage return \r and a newline \n or just a newline. Generally, I have found you can just do \r\n

Also depending on if you want the entire screen to clear in between "loading"s you might have to call the system call (and this differs based on platform) to clear the screen in between "loading"s being printed. This would negate the need to call \r\n because each time you print it is to a fresh console window.

You can look here for how to do this.

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46