Home > OS >  Python prevent print Duplication
Python prevent print Duplication

Time:01-29

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?

CodePudding user response:

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)

  •  Tags:  
  • Related