serwa
serwa

Reputation: 19

python loops in one line

so I'm pretty new to python and I tried to make this loading screen thing:

import time

loading1 = "loading."
loading2 = "loading.."
loading3 = "loading..."
random1 = 0

while random1 < 10:
    print(loading1 + "\r", end = "")
    time.sleep(1)
    random1 += 1

    print(loading2 + "\r", end = "")
    time.sleep(1)
    random1 += 1

    print(loading3 + "\r", end = "")
    time.sleep(1)
    random1 += 1

but it keeps hanging after the first 'loading...' but it should repeat 10 times, what am I doing wrong here?

Upvotes: 1

Views: 89

Answers (1)

Barmar
Barmar

Reputation: 780688

It only looks like it's hanging, it's actually just writing loading. on top of loading... when you go back to the beginning of the loop. But nothing removes the extra dots after it, so you can't see the difference.

Add spaces to the end of loading1, so that when you repeat the loop it will clear away the second and third . from the previous iteration.

import time

loading1 = "loading.  "
loading2 = "loading.."
loading3 = "loading..."
random1 = 0

while random1 < 10:
    print(loading1 + "\r", end = "")
    time.sleep(1)
    random1 += 1

    print(loading2 + "\r", end = "")
    time.sleep(1)
    random1 += 1

    print(loading3 + "\r", end = "")
    time.sleep(1)
    random1 += 1

Upvotes: 3

Related Questions