Question

I'm just learning Python and I have tried this simple loop based on Learn Python The Hard Way. With my basic understanding, this should keep printing "Hello", one letter at a time at the same position. This seems to be the case, but the print is not fluid, it doesn't spend the same amount of time on each character; some go very fast, and then it seems to get stuck for one or two seconds on one.

Can you explain why?

while True:
    for i in ["H","e","l","l","o"]:
        print "%s\r" % i,
Was it helpful?

Solution

you are running an infinite loop with very little work done in it, and most of it being printing. The bottleneck of such an application is how fast your output can be integrated in your running environment (you console).

There are various buffers involved, and the system can also schedule other processes and therefore pause your app for a few cycles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top