How to create a "TypeWriter"
Have you ever seen those programs, in which the output is "typed" out? Like, each letter is printed out one by one? If you have, and would want to know how to make it? Well you have ended up in the right place!
Ok, no more talking; let's get right to the code.
So first of all, we need to import the modules:
One module, actually
Now, we need to define our function; I have named it
printt(), but you can name it whatever you want:
If you have noticed so far but don't know, the
stringin the brackets of our defined function, that is a parameter. It is uesd as a placeholder for whatever we qant variables/string/numbers. Same for the
delay. Delay pauses the
Ok, so far, we have our modules imported and our function defined. Now we can actually start coding.
So, we need a
for loop to print out every letter.
I will be using the letter
i for every character in the string and
string as the string/variable to be "typed" out
Next we will put this:
If you're stumped as to what
flush are, they are actually parameters for the
These parameters are actually optional, so you won't need to put them to use the
We put the
end parameter there without anything in the quotation marks to indicate there is no end, so it keeps on printing from the line before, and
flush, I honestly dont understand it still.
flush is when you print something to the console text gets built up over time, but the new text isn't constantly rendered on the screen, instead its regularly "flushed" to the screen.
printt() prints stuff to the console faster than it can render text, so by putting
flush = True in your print statement you're forcing it to render the text in time.
Thanks to @Dunce for heling me out for
sleep() function is from the
time module, and is used to "pause" the code for a few seconds. Here,
sleep() "pauses" the program for 0.05 seconds.
So far we have this:
Next, we add a
print() statement to print a new line after the string is typed out:
AND WE ARE DONE!
That's it. I know its simple, but it's worth it as it makes the program less "dull"
Please check out my Racing Simulator
And be sure to check out my next tutorial: sep = ..., end = ..., and flush = ...
Please give me more ideas on what to code, I don't have no more ideas.