0

I tried to make a function which can "spit out" given string, character by character. It works the way it should, except for one part. The way it's supposed to work includes clearing the console, yet it doesn't want to work. I tried various methods, like using os module to call "cls" command, yet it didn't help. After i compile the code and try to run it in a windows console, not a single character shows.

import os
from time import sleep

def text_function(text): 
    def clear():
        os.system("cls")

    def length(to_measure):
        true_length = len(to_measure)
        return true_length

    text_length = length(text)

    separated = list(text)

    already_printed = []
    already_printed.append(" ")

    number = 0

    while True:
        already_printed.append(separated[number])
        sleep(0.15)
        print(*already_printed, end="")
        print("|", end="")
        sleep(0.15)
        clear()
        number = number + 1

        if number == text_length:
            break


print(" ")
text_function("TEST INPUT")
sleep(3)

Thank you in advance.

DNM24
  • 25
  • 1
  • 5
  • Ideally, we want only one question to a question. Is your problem that nothing prints, or that `clear()` doesn't successfully clear the screen? (If you have both problems, ideally, solve them one at a time -- starting by looking for preexisting answers to each of them individually). – Charles Duffy May 27 '20 at 02:06
  • BTW, while my knowledge of default buffering rules doesn't necessarily apply to Windows, on UNIX stdout is line-buffered, so unless you `flush()` it, nothing is guaranteed to be printed until you write a newline. The `end=''`s suppress newlines, so this code is never guaranteed to print anything (if the normal UNIX rules apply). – Charles Duffy May 27 '20 at 02:07
  • 1
    ...so, you _may_ just need to add a `print("")` before your `sleep()`. – Charles Duffy May 27 '20 at 02:07
  • For the content-not-printing part, see [Why `print` content doesn't show immediately in terminal](https://stackoverflow.com/questions/25897335/why-print-content-doesnt-show-immediately-in-terminal), or [Print without newline under function doesn't work as it should in Python](https://stackoverflow.com/questions/5101151/print-without-newline-under-function-doesnt-work-as-it-should-in-python) – Charles Duffy May 27 '20 at 02:10
  • Sorry for not making it precise enough. The first option. The strings should appear one after another, each longer by one character than the previous one, with screen clearing in between. I can't see anything. – DNM24 May 27 '20 at 02:10
  • 2
    Does adding an explicit flush, or a print of an empty line, or just taking off the `end=''` after the last print call before sleeping, solve your problem? (If so, we can safely close it as a duplicate of one of the questions in the second set I linked). – Charles Duffy May 27 '20 at 02:11
  • ... Yes, yes it does. Thank you so much, Sir. – DNM24 May 27 '20 at 02:14

0 Answers0