0

I would like create a simple loading screen using just print, something like this:

for x in range(5):
 print("Loading...")
 sleep(1)
 os.system('cls')

How would I add more and more fullstops each time for 5 seconds? Is this attempt even close? Thank you!

0

5 Answers 5

1

Although vaguely you could use something like

seconds = 1
while():
    i = "." * seconds
    sys.stdout.write("\rLoading {}", %i)
    seconds += 1
    if seconds == 4: seconds = 1
    time.sleep(5)

but i would highly suggest you use tqdm which will have almost every case you want to use.

Sign up to request clarification or add additional context in comments.

Comments

0

Without knowing what else is going on in your application or how it is structured; you can start with your loading message and then add what you want on each iteration of the loop. Something like this:

import os
from time import sleep

msg = "Loading.."
for x in range(5):
  msg = msg + "."
  print(msg)
  sleep(1)
  os.system("cls")

print("%sDone!" % msg)

3 Comments

Hey! Thank you! What exactly does the % do?
Glad to help! The % is for string formatting with variables, one %s for each variable. So if you had multiple variables you would write it like print("Name: %s %s" % (firstName, lastName)) There is some reference reading here pyformat.info
you don't have to use os.system("cls") you can simply use print like this: print(f"\r{msg}", end='')
0

Using fstrings, you can update the number of full stops by multiplying with your x in the loop

import os
from time import sleep

for x in range(5):
    print(f"Loading{'.'*(x+1)}")
    sleep(1)
    os.system('cls')

Loading.
Loading..
Loading...
Loading....
Loading.....

Comments

0

I am not sure about what you mean by "fullstops", but here is something that can help you:

import time
print('Loading', end='')
for x in range(5):
    print('.', end='')
    time.sleep(1)
print('')

The output will be iteratively on the same line:

Loading
Loading.
Loading..
Loading...
Loading....
Loading.....

If you want to create a loading bar you can use the package LoadBar.

pip install load-bar termcolor
import time
from loadbar import LoadBar

bar = LoadBar(
    max=5
)
bar.start()
for x in range(5):
    bar.update(step=x)
    time.sleep(1)
bar.end()

The output will be iteratively on the same line:

0/5 (  0%) [.                   ] ETA -:--:--
1/5 ( 20%) [.....               ] ETA 0:00:04
2/5 ( 40%) [.........           ] ETA 0:00:03
3/5 ( 60%) [.............       ] ETA 0:00:02
4/5 ( 80%) [.................   ] ETA 0:00:01
5/5 (100%) [....................] Time 0:00:05

Comments

0

This is not a direct answer to your practical question, but I would consider using tqdm.
With this module you can create a progress bar which I find more elegant than using these print statemenets.
Try this:

from tqdm import trange
from time import sleep
for i in trange(100):
    sleep(0.01)

You can look at the docs for more examples here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.