Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python prevent print Duplication

from time import sleep
from os import system

clear = lambda:system("cls")

clear()

Greetings = """Hello Hooman (Lemme be ur Wooman), SIKE! That's the Wrong Number(Ooooooooooooooohhhhhhh). My Humour is Broken, I Apologise for this Inhoomanly Behaviour. So You are also here to get the Prophecy, you want to know your Percentage I see, My All Seeing Eye knows Everything."""

x = len(Greetings)
Loop = 0

while x!=0:
    print(Greetings[:Loop], end = "\r")
    sleep(0.1)
    Loop += 1
    x -= 1
    clear()

print(Greetings)

In the Above code, the use of clear() in the loop slows down the printing of the sentence added to the fact that it keeps flashing.
But if I don’t use clear(), then as soon as the Sentence Reaches the end of Line, it starts Printing it like 10 times, You might understand what I am trying to say once you run the code without clear().
It is because how \r works. So is there a way to print it without any duplication problems and also not use clear() to speed up the printing process?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use end='' to not create a new line and flush=True to flush the output buffer every time you call print().

You can call clear() at the beginning of the programme to clear the screen.

from time import sleep

Greetings = """Hello Hooman (Lemme be ur Wooman), SIKE! That's the Wrong Number(Ooooooooooooooohhhhhhh). My Humour is Broken, I Apologise for this Inhoomanly Behaviour. So You are also here to get the Prophecy, you want to know your Percentage I see, My All Seeing Eye knows Everything."""

for char in Greetings:
    print(char, end="", flush=True)
    sleep(0.1)

print(Greetings)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading