TypeWriter
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:
from time import sleep
One module, actually
Now, we need to define our function; I have named it printt()
, but you can name it whatever you want:
def printt(string, delay): # Our code will go here # Our code will go here # Our code will go here
If you have noticed so far but don't know, the string
in 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 print
function for a certain amount of time so it makes it look like a "typewriter"
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
def printt(string, delay): for i in str(string): # Our code will go here # Our code will go here # Our code will go here
Next we will put this:
def printt(string, delay): for i in str(string): print(i, end="", flush = True) sleep(delay)
If you're stumped as to what end
, and flush
are, they are actually parameters for the print
function. While you're coding, hover your mouse over the print
function and it will show this:
These parameters are actually optional, so you won't need to put them to use the print
function.
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.
EDIT: 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.
But 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 flush
The 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:
from time import sleep def printt(string, delay): for i in str(string): print(i, end="", flush = True) sleep(delay)
Next, we add a print()
statement to print a new line after the string is typed out:
from time import sleep def printt(string, delay): for i in str(string): print(i, end="", flush = True) sleep(delay) print('') # Not Inside the for loop, or it will mess up the program
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.
That's it for this Tutorial and see you in my next post; Bye!
HOW did this get soooooo many votes? This is only 9 lines of code :(
@codingjlu lol that's what I'm saying: I go to bed and then the next day I wake up there's like 10 more upvotes
@CodingEssence I don't know why :/ I feel the community is a bit... blah xD they can't tell what deserves to be upvoted and what doesn't
@codingjlu what about my flask tutorial?
@ruiwenge2 link pls?
@ruiwenge2 not that I'm trying to offend anyone. If I did, I'm sorry; I just think it's a little amazing that this tutorial could get so many votes!
@codingjlu oh it’s in the hot section
@ruiwenge2 oooh nice
@codingjlu thanks! :)
@ruiwenge2 is flask like HTML but in python?
@CodingEssence there's no HTML replacement :/
@codingjlu but what is flask?
@CodingEssence do you do Node.js? Flask is a web server library for Python.
@codingjlu Oh, that's what flask is, and no. I don't do Node.js. I used to know the basics... and forgot everything and only remember console.log()
and var
/let
XD
@CodingEssence lol don't forget const
@codingjlu I just remembered that as soon as you said it. oof
@CodingEssence lol okay
@codingjlu i dunno
Good post, it's nice to see a tutorial that isn't just a "Python basics" tutorial. But 1 little nitpick I have...
def printt(string, delay = 0.05):
Delay doesn't have to be 0.05, it can really be anything. The average reader reads around 250 WPM which is around 20 chars per second, which is good, but that does not account for people who are insane and read crazy fast and enjoy binge reading entire series over a single weekend who can read fast or people who read slow.
who are insane and read crazy fast and enjoy binge reading entire series over a single weekendlol
I have had that in my mind while I was creating this; you can actually change the delay while you are coding bc it is "optional", meaning if you don't put any delay when you use that function, it will automatically default to 0.05, or if you did, it would default too that, or you could give an input at the very beginning of the code, which is what I might do in my next project. Thanks for reminding me about that
@CodingEssence Exactly ;). basically instead of
def printt(string, delay = 0.05):
maybe do
def printt(string, delay = #DELAY_HERE):
@FlaminHotValdez Ok, I'll put that there; thanks!
@FlaminHotValdez I would prefer 0.03 as the delay if the text is large
Hahaha simple :)
yo man very helpful
Learning to program can be difficult, and students may require programming assistance. Many websites provide programming assignment help, and these websites provide useful tips and tactics to help students learn programming quickly.
No advertising please! @kaylee402
We offer assignment help Australia in almost every subject, thanks to our expert writers. Completing assignments is an important aspect for any student.
Is there a way to make this for input as well?
@OrangeChromium Yes!
You could do this:
def inputt(string, delay): for i in str(string): print(i, end="", flush = True) sleep(delay) return input('\n')
and you would use it like this:
your_variable = inputt('My String', 0.08)
People who think this is just easy to make, this is not the case. This tutorial is very helpful to beginners starting to code in Python.
Hi, I am Mason Ethan, a thesis proposal help expert. From the last 5 years I am providing thesis proposal writing help for the students, according to the review I am getting average 4.2 ratings for every student. If you also need help with your thesis proposal then you can visit our website. You can also connect me to the phone for instant response.
This is very useful! Thank you
It's nice. Honestly I didn't undestand flush() but now its OK. Thanks.
Typewriter and word counter are two tools I use often, it's great guys word counter online
Thanks for sharing. It looks like a good thread with lots of information. I will bookmark this thread for further reference. In the meantime, I also need someone who can work for me as a plagiarism fixer. I have heard about Study Clerk in the past and now I want to give it a try here https://studyclerk.com/plagiarism-fixer What do you think? Will wait for your further reply.
Thanks!
I am going to use this tutorial soon. thanks!
nice
another idea: Use a different delay between each character.
You could
- randomize the delay (this is easy and requires no storage, but is also not super-realistic: it is only a slight improvement over the fixed delay)
- create character pairs and randomize the delay between each pair
- record the delay from someone actually typing (biometrics: each person has a unique typing delay profile.)
mine is buffering, I'll give you 1 upvote!
@ChristianDowell maybe its your connection, but thanks!
@Cooli @CodingEssence bruh stop updooting each other
@CodingEssence yeah dude u have so many cycles from this one tutorial. u know what, ima gun for ur spot as the current top tutorial
@CodingEssence @Cooli you both realize u said the exact same thing? its like y'all are siblings... so stop LOLing everything. what the hack is replit going through.
@CodingEssence stop stop stop im joking im joking! (meme)
@Cooli hey cooli, u only have 3 cycles now, what happened?
@Cooli and updooted u right back ur at 4 now yay ima updoot every repl I find that you've posted so u can get more updoots since u really deserve them
@Cooli yeah if u check out my bio theres not much since im on a new account I deleted the last one my projects suck :(
@Cooli yeah im often bored so i play amogus then code but run out of ideas... BUT Im REALLY active on repl talk
very hard
mmm... croissant like
Any of y'all notice @CodingEssence just changed his bio from 12 year old to 13 year old? Well, I did, so happy (late) birthday!
Thank you so much! I added this to my project if you don't mind.
Have a great rest of your day! :D
@MEadphonesmead It's okay if you add it; it's meant to be used :P
And you also have a great tomorrow. lol
@CodingEssence thanks lol
Realy coll. That is my first week, I am liking realy, realy
@Jacintohelder3 Thanks!
Nice! The only problem is that it doesn't add a newline at the end, so with the code:
printt("Hello", 0.05) printt("world!", 0.05)
the output looks like this:
Helloworld!
to fix that just change your code to this:
from time import sleep def printt(string, delay): for i in str(string) + "\n": print(i, end="", flush=True) sleep(delay)
Also you said you didn't understand flush
, well 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.
But 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.
@CodingEssence Sure!
Even better solution:
from time import sleep def printt(string, delay): for i in str(string): print(i, end="", flush = True) sleep(delay) print('')
@CodingEssence It seems more readable, and easier to just manually add a newline character to me :)
syntax error
can anyone make me understand the code clearly as I am a student of middle school pls anyone:]
Very helpful for the new game I am creating!
@Cooli btw I gave you 100 upvotes
@Cooli Wow, thanks. I nearly had a heart-attack when you said 100 upvotes. im thinking, "199 cycles... what the-". lol
@CodingEssence Can you upvote me I only have 1, plz?
@Cooli Lol fine
@Cooli xD here's another upvote bro, have fun. also what game are you making?
@Ezyh idk my school is about to start in 3 weeks so I am just getting ready, currently trying to update my website! Here is the link: https://bit.ly/2VvLoid