0

I'm practicing creating classes and functions in Python. In a certain simple countdown exercise, I had to use the sleep() function inside a FOR loop to display the numbers one by one. The issue is that the output performs the count, but without showing the number corresponding to the thread of the FOR loop. In the end, the numbers appear all at once in a single print. The code is below:

import closed
from time import sleep

class Countdown:
    def __init__(self):
        pass

    def Count(self):
        for counter in range(10,-1,-1):
            print(f'{counter} ', end='')
            sleep(1)
        
        return 'Finished Counting!'


countdown = Countdown()
print(countdown.Count())

Just to inform you, import closed only serves to show a thank you message at the beginning of the program execution.

I tried taking the FOR loop from the Count method and putting it in the class call in the main part of the code below, but I got the same behavior. Thanks in advance for the responses.

  • Related? https://stackoverflow.com/questions/230751/how-can-i-flush-the-output-of-the-print-function – slothrop Jun 15 '23 at 13:05
  • Werid I copy pasted your code and it does work, it print number from 10 to 0 with an interval of 1 second, then it print "Finished Counting!" – Nathan Jun 15 '23 at 13:10
  • Also related? https://stackoverflow.com/questions/25897335/why-doesnt-print-output-show-up-immediately-in-the-terminal-when-there-is-no-ne – slothrop Jun 15 '23 at 13:11
  • Serious? Now the doubt about what the core problem is has increased. Does it have something to do with the IDE used? I'm editing in VS Code on Windows 10. – Sérgio Lima Jun 15 '23 at 13:15
  • The only difference is I did not copy the `import closed`, I also tried it in VS Code – Nathan Jun 15 '23 at 13:31
  • Does this answer your question? [Why doesn't print output show up immediately in the terminal when there is no newline at the end?](https://stackoverflow.com/questions/25897335/why-doesnt-print-output-show-up-immediately-in-the-terminal-when-there-is-no-ne) – Jeanot Zubler Jun 15 '23 at 13:33
  • @Nathan Sorry for the delay in responding. I just realized that this problem only occurs when I use the end=' ' parameter at the end of the input() so that there are spaces between the printed numbers. However, when I remove it, the count is performed normally with the time established in sleep(). The problem is linked to the end= parameter, in this case. – Sérgio Lima Jul 11 '23 at 14:33

0 Answers0