The Best Python Project For Beginners! (Full Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome to another youtube video so in today's video i'm going to be sharing with you an awesome beginner project in python that is going to be a typing speed test now the idea behind this is you're going to be given a block of text you're going to be asked to type this as quickly as you can and then while you type it's going to record your words per minute and when you finish the text it's going to tell you how quickly you typed it this is a really awesome application really awesome project to work on and i'm going to show you how to do a ton of styling in the terminal so how to do something like clear the entire terminal right over a line that's already in the terminal change the color of your text change the background color of your text all kinds of stuff that you normally never do when you're working with a console-based application and i'm just very excited about this project because i know it's going to give a ton of value to a lot of you guys now i'm going to give you a demo in just one second but i want to quickly mention that this tutorial is designed for beginners so people that have maybe a little bit of knowledge in python have messed around and used things like for loops maybe functions variables they kind of have a general idea but are definitely not experts and are looking for some good kind of practice projects now if you're intermediate or advanced in python feel free to follow along as well you'll probably still enjoy this project but it really is kind of tailored towards the beginners looking for something that's decently large this is going to be about 100 lines of code to really work on their skills and teach them a lot anyways with that said let's look at a quick demo and then we'll get into the video so you can see here i have my terminal open it says welcome to this typing test you'll be given a block of text i need to type it as fast as possible press any key to start so i'm going to start and it's going to bring up the text like this now i'm not going very fast right now but the point is that we can just type the text and if i get something wrong so notice i'm getting something wrong here it's highlighting in red so this is something that you know you probably don't already know how to do and that is pretty simple actually but i'm going to show you how to do in this video how to get all the text highlighting and kind of nice styling as you see here and then the ability to clear the entire console so you can see here it says you completed the text your wpm was 55. so i'm going to hit enter and it says you want to play again let's play again brings up some text it actually does randomize the text but in this case we just got the same text that we had before anyways there you go that is the demo of the project now we're gonna hop into the code after a quick word from our sponsor before we dive in i need to thank ltm designer for sponsoring this video ltm designer is the world's most trusted pcb design system that enables engineers to effortlessly connect with every facet of the electronics design process ltm designer has used 35 years of innovation and development to create a truly unifying design environment that makes it the most used pcb design solution on the market ltm designer provides an intuitive and powerful interface that lets you design pcbs rapidly while interacting and collaborating with your mechanical designers the interface provides a photorealistic 3d environment collaboration and synchronization with tools including solidworks ptc creo and autodesk inventor realistic rigid flex designs multi-board assembly and much more ltm designer is the most popular ecad tool and electronics design software and you can get started with it today for free by pressing the link in the description and registering for a free trial thanks again to ltm designer for sponsoring this video and now let's dive in all right so let's go ahead and get started i'm here in my text editor i'm going to be using sublime text for this video not really that important you can use whatever you want anyways what i've done is opened up a folder and then inside of this folder i have a file called tutorial.pi i'm going to recommend you the same thing make a folder somewhere probably put on your desktop put a file inside of there that is a python file and then you are good to start working now as you guys saw we had some really nice styling for our terminal we had some different colors and we were able to kind of write over the previous text now the way we were able to do that was by using this module called curses now this is built into python if you're on mac or linux but if you're on windows you actually need to install it so if you're on windows you need to follow along with this step otherwise skip forward into the video until we actually start writing some code okay so if you guys are on windows what you need to do is open up your command prompt here and we're going to type the following command to install the windows version of this curses module i'm going to say pip install and this is going to be windows hyphen curses like this so you should run this command and it should install this for you and if for some reason this command does not work for you i will leave two videos on the screen they'll be linked in the description as well and they'll be cards on the screen showing you how to fix this pip command now they're called how to install pi game just follow along with the exact same steps except instead of installing pi game you're going to install windows curses now you also can just try here pip install and actually sorry this python hyphen m pip install and then windows hyphen curses like this and see if this command works for you and if this doesn't work then again follow along with those two videos anyways this should install the curses module and once you have the curses module installed then we are ready to go okay so i have curses installed and this is what allows me to do the styling of the terminal so the first thing i want to show you how to do is just to use this module and to kind of print stuff out to the terminal because it's going to be different than what we typically do we're not going to be using the print and the input statements we're going to be using some statements from this module okay so the first thing i'm actually going to do here is write another line i'm going to say from wrapper or sorry not from wrapper from curses import wrap now what this is going to do is give me something that allows me to initialize this cursus module because when i initialize the module what's going to happen is it's going to take over my terminal and it's going to allow me to kind of run some different commands on it so i use this wrapper it initializes curses and then as soon as i'm finished with the program it will kind of restore the terminal back to its previous state so it's pretty important that you do this anyways what i'm going to do here is say define main and inside of main i'm going to put a variable here which is going to be called std scr standing for std screen now std is your standard output so the standard output is just kind of the terminal it's where you're writing stuff out to and what this module does is kind of take that over and give you a screen over top of it that allows you to write stuff to the screen anyways that's what this is doing so it is the standard output screen okay perfect so now that we're inside of here for now what i'm going to do is show you the most basic thing which is just adding text to the screen the way you do this is you say std scr dot add and then string like that so then inside of here in quotation marks you just put the text that you want to print out to the console so just like a print statement i'm going to put the text hello world okay that's it this is just going to output the text for you then what i'm going to do here is i'm going to call wrapper and then pass my main function now this might look a little bit confusing what i'm doing is i'm passing this main function to this wrapper function so this is actually a function i just call it like this using the parentheses i pass main inside of there and it's going to actually call this function while initializing all of the stuff related to this module so anyways that's the first step all this is going to do is print hello world out to the screen now before i do that though i actually want to clear the entire screen because if i don't clear the entire screen we're going to have a bunch of junk on there so what i want to do is i want to say std scr dot and then clear this just clears the entire screen for me and then as soon as i do anything so i add text i need to refresh the screen so the way i do that is std scr dot and then refresh like that perfect okay so this will actually print hello world out for me all right so now that we have all of this i actually just want to add one more line here i'm going to say str or sorry std.scr dot and then get key like this with two parentheses what this is going to do is just wait for the user to type something so that way it doesn't immediately show this and then close the screen because right now if we run this code what's going to happen is it will print hello world it will refresh we'll see it for like a millisecond and then it will end we need to add this getkey so that we don't immediately close the program we wait for the user to type something and then we finish it okay so now that we have that we can run this code now to run this code is a little bit trickier than your standard python code you actually need to run it from your terminal or your command prompt that's because this requires a terminal or command prompt to actually show you the correct output so what we're going to have to do here is open up a command prompt or terminal again depending on your operating system we're going to have to get our working directory which is right here on the left hand side into the folder where this file is i'm going to show you a shortcut on windows here to do this and on mac i'll describe kind of how you can do the shortcut obviously i'm not on mac so i can't quite show it to you okay so i'm going to open up my windows explorer here and i just have it on my other screen right now i just need to hide some of my files and what i'm going to do is navigate to the path where my file is so i assume you know where your file is i mean you should know where you placed it anyways you can see here that my tutorial file is directly inside desktop and then beginner project so what i'm going to do is click on this menu bar here so notice where it's showing the path i'm just going to delete all of this and type cmd now when i do that it's going to open up a command prompt and notice the working directory which is on the left here is directly inside of beginner project so all i'm going to do now to run the code is type python and then the name of my file which is tutorial.pi now i'm going to hit enter and when i do that notice it shows me hello world and when i hit enter again it brings me back to my terminal okay that's how you run the code now if you're on mac you can do a very similar thing you can open up your finder you can navigate to where your python file is and then you can right click and there should be something that says open terminal now if you can't find open terminal what you're going to have to do is use the following commands cd like this so cd stands for change directory and to go to the parent directory you type cd dot dot so notice that brings me to desktop that's the parent directory of beginner project then if i wanted to get into beginner project i would type cd and then beginner project like that it would bring me into that directory so from wherever you are you need to change your directories into the correct directory where your python file is and then type python space and then the name of the python file you want to run now for some reason that doesn't work type python3 and then the name of the file so for me it's tutorial.pi but for you guys it will be probably something else hopefully that clears it up it's hard to explain that more because i'm not actually on mac but you need to open up a terminal and get this directory here on the left hand side to be the directory where your python file is or what you can do is you can look for the exact path of your file and type python and then put the exact path so it would be on windows something like c colon slash another path to it or on mac maybe python 3 and then the path to the file anyways that's pretty much all that i can explain for right now but that is how you run this code okay so now that we know how to run this let's talk about how we can add some colors here and kind of style it a little bit alright so if we want to add color to our text what we need to do here is create something known as a color pair now a color parish is the foreground and a background color so the foreground is the text the background is obviously what's behind it so what i'm going to do is i'm going to create a pairing of a foreground color and a background color and then use that pairing to print out my text so i'm going to say curses dot and then a knit underscore pair at the top of my function here and it's important you do it in the function if you try to do it outside it's not going to work you can only do this once you've initialized curses and that's going to happen when we call this wrapper function and then it calls the main function anyways i'm going to say curses.init pair i'm going to put the id of my pair which is going to be 1 and then i'm going to say curses dot and then this is going to be color underscore and then whatever the color of the text is that i want so in this case i'll go with green for my foreground color and then for my background color which is the second color you pass i'm going to say curses dot and then we'll go with color underscore and let's just go with white so that we can see kind of you know a lot of color here okay so what i've done is i've said that i want to have the pairing with the foreground color green and the background color white be represented by the id1 so i need to put an integer here and this integer kind of represents this pair so if i wanted to make another pair then what i could do is the same thing and i would just change the integer here to be two if i had it at one then it would override what this pair was so i wanted to change it to two and then i could make this something like color dot and then can we go with yellow let's go all capitals yellow and see if that's a valid color okay so now we have a pair with one and two as the id so to use this what i'm going to do is go inside of my add str here and i'm now going to reference this pair now the way i do that is i say curses dot color underscore pair like this and then inside of the brackets i just put the id of the pair i want to reference so if i want it green and white then i put one if i wanted it yellow and white then i would put two and if i just wanted the default color i just remove this i don't need to include anyways let's give this a test here and see if this is going to work i'm going to go to my terminal and go python tutorial.pi and notice we get green text with a white background then if i hit any key it's going to escape and bring me back to the term perfect let's try this now with the color pair 2 and run this and notice we get yellow text on a white background that looks pretty ugly but that is fine so for now the pairs that i want to initialize are actually just going to be red let's go with blue no do you want blue no let's just go with red green and white and then we can use those pairs throughout the program so i'm going to say curse is not a knit pair one green is perfect but for the background i want the background to be black then i want one that is going to be red so i'll say color red and color black and then i want my last color just to be the standard white text so i'll go three and then this will be color white like that okay perfect and for now we don't need to add any color to that okay so now we have our three colors we have one which is green two which is red and three which is white so now that we've looked at the colors the next thing i want to show you is how we can locate text differently on our terminal or kind of place it in a different place based on where we want to put it so in this std scr.ad string method that's a mouthful what we can actually do is specify the position so kind of the row and column that we want to start this text at so i'll show you what i mean but for now if we go with something like 1 0. what this is actually saying is i want to go one line down and start at the zero with character so start all the way furthest to the left but one line down so you go kind of your row and then your column that's how this coordinate system works now you'll see very quickly how this works is pretty straightforward but if i run this notice that now we're just on the second line down in the terminal so it's very easy to kind of place text in different places now if i go and do this like 1 5 you're going to see that now we're going to be 5 characters over to the right so if i run this now we're one row down 5 characters to the right so we start writing from that place okay hopefully that is clear now let's just see what happens if i actually write something else so let's write this at like 1 and then 0 and see what happens when i kind of have two colliding pieces of text so let's do this let's hit enter let's run the code and notice that the hello world i wrote second kind of overrides what was already on the screen so that can happen and that will happen based on how you're writing stuff out and that's exactly what we're going to do to kind of have our green text go over top of the white text when we're trying to type out the target text when we're doing kind of our speed test right so i just want to show you that you can kind of pick the location on where you want this text to go you just pass before the text the coordinates so you go either the row and then the column that you want to place this in and if you just want to be at the top left you can just leave it you don't need to add anything or you can explicitly say zero zero like that okay so now we have pretty much gone through everything in the curses module the last thing i'll show you is that we can actually get the key that people type so notice here that i said std scr.getkey so what i can do is store this in a variable and just print this out and now this will actually tell me the key that the user typed in and you'll see why this is important in a second but we're going to need to register the user's keystrokes so that we can see what they're typing and keep track of their word per minute right so that's what key will do for us we'll get the key that the user types so let's go to our terminal looks like i closed it so i'm going to have to open it again okay so i've got the command prompt open now and i'm just going to run this code so pythontutorial.pi i'll hit the key e and then notice it prints out e because that's the key that i hit okay now we've gone through the curses module now i want to start kind of building this speed test or this game or whatever you want to call it so the first thing i want to do is kind of have a start screen here that says hey would you like to play the game and if they say yes or they hit any key then we will you know like continue with the game and we'll get into the game so i'm going to write a function here i'm going to say define start underscore screen and this is going to take the std scr and the reason we need to take this is because we need to have access to this object to be able to write to the screen because notice we're using the add string method we're using clear we're using refresh so we just we need access to this to be able to actually write something onto the screen using the cursus module anyways what i'm going to do is i'm going to steal these lines right here so clear add string and refresh and i'm just going to put them inside of start screen and what i'm going to say is press any key to continue and i'll give kind of a description of the game so i'll say welcome to the speed typer test or whatever speed type or test speed typing test maybe and then we can do a new line so let's get rid of this here and let me show you how we can print something on a new line so we can use the coordinate system we can do something like one comma zero or what we can do is we can print this character which is a backslash n now when you print a backslash n what this does is it brings the cursor down to the next line and you're going to start writing wherever the cursor is so after i print this out my cursor is going to be at the end of this line and if i were to print something else so i were to do this line here it would go right after it kind of be squished together but if i wanted to go down to the next line i can put in this escape character called backslash n this is the new line character and it makes me go down to the next line before i start typing so now i'll say press any key to begin okay welcome to speed uh typing test press any key to begin boom and then what i will do is refresh the screen and i will just say key is equal to this and actually don't even need to store the key i just want to wait for the user to type something so that's what i'll put right here okay that's awesome so now what i can do is just call this start screen function from inside of this function right here so i'll call start screen and i'll pass the std scr like that and now what will happen is when i run the main function we'll initialize our colors we'll call the start screen we'll do this and then we'll continue so let's test this out before we go any further let's go python tutorial up hi welcome to speed typing test press any key to begin boom and then we begin okay so now that we've done that we need something to kind of store what our target text is we want to print that target text out to the screen and then ask the user to start typing it in so i'm going to make another function here and notice i'm kind of splitting all of my code up into different pieces of logic that's a good practice you should try to get in the habit of but that's what i'm trying to show you here anyways i'm going to say define wpm underscore test standing for words per minute test we are going to take the std scr like that and inside of here we're going to actually print out kind of what our target text is and then get the user to start typing it in so i'm going to say target underscore text is equal to and for now we'll go with hello world this is some test text for this app exclamation point and this is what we want the user to type in so we want to print this out and then we want to print whatever the user has typed kind of over top of it which you'll see in a second so now i'm going to say currenttext is equal to and i'm going to make a list you'll see why we need this in a second and for now all i'm going to do is clear the screen refresh it and then uh actually print what this text is so we'll start with that and then we'll kind of keep track of what the users actually typed so i'm going to steal again these lines because i'm just going to keep reusing them and i'm going to say stdscr.ad string and rather than welcome to speed typing test we'll just put the variable target text in here because this is what we actually want to display so we're going to get the target text which is this we're going to have our current text which will be what the user has typed we're going to clear the screen add this to the screen and then refresh and then again we'll just get key okay so now after we get past the start screen what we want to do is call the wpm underscore test function like this and pass this okay so let's give this a run and see how this is working python tutorial up hi let's hit enter node says hello world this is some test text for this app and when i hit enter or any key for that matter then it exits great so now that we've done that what i want to do is actually register the user's key presses and then i want to take whatever they're typing and kind of overlay it on top of this so as you saw in the demo if they type the character correctly i want it to show up green if they type it incorrectly i want it to be red okay pretty straightforward so what i'm going to do here is i'm going to start a while so i'm going to say wow true and i'm going to continually kind of ask the user to type something in and every time they type something i'm just going to overlay it in a different color on top of the target text so we're going to have to change this drastically from what i do right now but this is just an example so you can see how this is going to work so what i'm going to do is say key is equal to and then std scr dot and then get key which means i can remove this one right here because we're immediately going to jump into the while loop and then we'll get the key then i'm going to add this key to the current text and i'm going to draw the current text onto the screen so i'm going to say let's go current text like this dot append and i'm going to append the key so we're going to have a list of all of the keys that the user has pressed then i'm going to loop through every single one of these keys using a for loop and i'm going to kind of place that character onto the screen so i'm going to say 4 and we're going to go with char standing for character in and this is going to be current text what i'm going to do is say std scr dot and then this is going to be add string we're going to add whatever this character is comma and then i'm going to put the color that i want this to be so in this case i want the color to be let's go with green so that's going to be pair one so what do i need to do i need to say curses dot and then this is going to be color underscore pair 1. okay so let's just go through this quickly to make sure everything is clear so what we're doing is we have the target text we have the current text we are clearing the screen we are adding the target text to the screen we're then refreshing the screen we're then going to ask the user to enter a key so when i say get key this is going to wait for them to type something as soon as they type that we're going to append that to the current text then we're going to loop through every single character that they've typed because they're going to be storing that in a list so for char in current text we are then going to display that character on the screen so we're just going to add it and it will be in a different color okay now one thing that i want to do here is i want to constantly do this so you'll see why i want to do this in a second but i'm going to go here and i'm going to add this string the target text and then after i add the other string i'm going to refresh so i'm going to clear the screen every single time this while loop runs i am going to add the target text to the screen i am then going to add the other text onto the screen so whatever the user has typed in a different color and then i'm going to refresh the screen so that you see it so this way i'm not going to continually keep adding the same text to the screen i'm going to clear everything and then add whatever i currently have if the user adds another key i'll add that as well you'll see why i need this but i'll show you if i don't clear what's going to happen in one second anyways let's run this and let's just see what's happening so it says press any key to begin okay so hello world this is some test text for this app and now let's just type something so let's go h and notice it's showing up on a new line all of the characters that i'm typing so that's exactly what i expected now if i didn't clear the screen what would happen is we would write all of this like multiple times onto the screen so let me show you this i'm just going to get out of this actually we didn't even make a way to exit this so i'm going to hit ctrl c okay that doesn't work we're going to have to just close the terminal because i didn't make a way to exit yet well we'll have to do that in a second anyways if i don't clear the screen here and we rerun this so let me go to my terminal so cmd cd desktop cd beginner project and pythontutorial.pi now watch what happens when i start typing okay so first of all you can see that it didn't clear but if i type this it's going to repeat all the text a ton of times right because we're not clearing what we previously had we just keep adding what we had to the screen so that's kind of what i was getting at that's why we need to clear so anyways let's make sure we include this clear and now what i want to do is include some way for the user to actually exit the program so i'm just going to check if the key that they hit was the escape key and if they hit escape i'm going to allow them to exit because obviously we want some way to get out of this program so before i add this to the current text i'm just going to check if the ordinal value of this key and i'll explain what that is in a second is equal to and then 27. now if it is i'm just going to break what break is going to do is just get me outside of this wallop so it will break this infinite loop now the ordinal value of a key is its numeric representation on your keyboard so every single key on your keyboard has some number that represents it and this number is known as the ascii representation or the unicode representation you don't have to really know what that means ascii stands for american standard something something something i think it's like american standard code information whatever someone smarter than me in the comments will tell you what it stands for i forget or maybe i'll throw it up on the screen point is every single character has some number that represents it so i know that the character lowercase a is represented by 97 uppercase a is represented by 65 and just so happens that escape is represented by 27. so we're going to check the ordinal value when you take the ordinal value of an individual character which is what the key will be it will give you what it is so if it's 27 then we'll break that means they hit the escape key okay hopefully that makes sense we can test that out though so let's open up our command prompt so let's cd to desktop cd to the beginner project and then go python and then tutorial.pi and now if i hit escape notice i'm going to exit and i am all good perfect that is exactly what i wanted now one thing i will do here is i'm going to show all of this before i ask the user for a key the reason for this is right now i'm having to hit the key kind of two times before it shows anything so i want to show this first and then ask the user to kind of type in a key and when they type in that key then we will add it to their current text now one problem that we're going to run into here is that there is a bunch of special keys that the user could hit for example the backspace if the user hits the backspace i don't want to display the backspace character i actually want to remove one of the characters that they typed now since we're kind of doing this in curses we have to manually handle all of this so when i hit backspace it's not just going to move me back it's actually going to show kind of a really strange character on the screen so let me show you what i mean but if i run this code so python tutorial dot pi let's hit enter and let's hit the backspace notice that it's bringing my cursor backwards but if i start typing stuff here it's not actually going to delete that stuff right it's just bringing the cursor back it's not actually deleting anything so when i hit backspace i want to delete the text that i was typed so let's see how we can do that so what i'm going to do now is after i check if the ordinal value of my key is 27 i'm going to say if the key is in and then i'm going to check three things now the first thing i'm going to check is key underscore backspace the next thing i'm going to check i have to actually look at here it's going to be backslash b and then the last thing is going to be slash x so backslash x sorry 7 and then f now i understand this looks like complete gibberish but the thing is the backspace key on different operating systems will be represented by a bunch of different characters so it could be represented by key backspace it could be represented by backslash b or it could be represented by backslash x7f don't ask me why that's representation but that's what it could be so just to make sure this is going to work for all of you no matter what operating system you're on just do this now this is going to check if this key is in this so essentially if the key is equal to any one of these three things if it is then what we're going to do is we're actually going to pop off the last element from current text you'll see what i mean here but i'm going to say if the len of current text is greater than zero than currenttext.pop now this is one of the reasons why we use the list here to represent all the characters that we're typing because we can very easily and quickly remove the last character from it so what dot pop will do is it will remove the last character from a list or the last element from a list so if i have one two and i call dot pop on this list it just gets rid of two so now i only have one which is exactly what we want because remember current text is keeping track of all of the keys that we have pressed so if we hit the backspace we want to get rid of the last key that we input that's what this will now do now we just need to put an else statement here because if we hit the backspace we don't want to add that backspace key to our current text we just want to remove the current character so we'll add the else to make sure that if this is the case we're not still adding this key to the current text okay hopefully that makes sense let's run this code and let's hit enter and let's start typing so i'm going to say hello world and then i'm going to hit backspace and notice when i hit backspace i'm actually deleting the characters now which is precisely what i wanted so now that we've done this let's see how we can get this text to go over top of the text that we currently have because obviously that's kind of what we want we don't want this text to be what do you call it coming after it wanted to kind of go over top of it and then we want to change the color on if it is the correct character or not okay so let me escape this by hitting escape and let's see how we can go about doing that so to do this i'm just going to write a new function here because i think it makes sense to kind of separate displaying all of our what do you call it text in a separate function so i'm going to say define display underscore text like this we're going to take in our std source like this or sorry screen not source we're going to take in our target text we're going to take in our current text and then for now we'll take in wpm as well but i'm going to make this a default value of 0. now in case you're unfamiliar with this these are parameters which means we have to pass values to this function when we call it and if you do something like equal 0 this means this is a optional parameter so if i don't pass this parameter it will just have a default value of 0. now the reason i'm doing this is because we're going to have to calculate the words per minute in a second and we want to display that as well won't do that in this step this second but we're going to do that later okay so now that we have that what i want to do is display the target text and over top want to display the current text so i'm going to take all of the stuff that i did here so let's go with this right here the add string and the character and i'm going to paste this like that so let's now go over here so let's say we're going to display the target text then we're going to loop through all the characters in our current text but i want to place them somewhere different i want to place them on top of this text so i'm going to say this for i comma char in enumerate current text now if you're unfamiliar with enumerate what this is going to do is it's going to give us the element from our current text as well as the index in the list so remember current text is a list let's say we have the elements 1 2 3 4 like that what's going to happen when i enumerate over this is i is going to start out being equal to 0 0 is going to reference this first index right here and then char is going to be equal to 1. now of course these would be characters but hopefully you get the idea so the next element would be 2. so chart would be 2 and i would be 1. this gives me the index as well as the element it's just kind of a cleaner way than just going by the index and then manually getting the element hopefully that makes sense but the thing is whatever character that i am on so if i'm on character 0 or character 1 or character 2 according to their indexes in the list i can use that to determine where i should place this character so i'm going to say 0 comma i like this so the point is i is going to keep getting incremented by 1. this is representing what character that i'm going to place this on and so i want to place it on the very first character which is 0 and then on 1 then on 2 then on 3 then on 4 so this will be overlaid on top of my target text hopefully that makes a little bit of sense but since i is going up by 1 in every single iteration this means every character that i'm drawing onto the screen will kind of be incremented by one and it will go directly over top of my target text so let me just show you and hopefully this will clear it up what i need to do though is i do need to call this function so right between my clear and my refresh i'm going to say display text and then i'm going to pass this all of the parameters so std scr i'm going to pass by target text and i'm going to pass my current text as well okay and then inside of here sorry i'm referencing target text when installing to be referencing just current okay hopefully that is all good now let's go ahead and run our code here so python tutorial.pi let's hit enter and now notice when i type hello that it's going over top of my text right and if i hit space of course it's going to remove those characters because well that's what it's doing but you get the idea okay and then i can go like this so now what we need to do is we need to change the color of this text based on if it's the correct character or not okay but you can see that it is going directly over the top of this text and even though it's kind of replacing the text it looks like it's just changing the color okay that's what we want so i'm going to hit escape and now we're going to modify the color based on if this is the correct character now we have what the correct characters are in our target text and then we have whatever our current text is so what we can actually do is we can compare every single character in our current text to the corresponding character in the target text and see if we're getting the correct one so i'm just going to say the correct underscore char like this is equal to the target at index i so if my target text is something like hello and then i have my characters as like h comma and then l like this if i'm accessing the zero with index so i'm looking at the first character i can look at the zeroth index in this string and i can check if the character i have here matches with that character if they do then i can show this in green if they don't then i can show this in red so i'm going to say that my color is equal to and then this is going to be curses dot color pair at 1 like that and then i'll say if the char is not equal to the correct char then the color is going to be equal to curses dot color pair and then what do we use for red i got to see this we use 2. so let's go here and do two and then i'll put the color variable so let me slow down to make sure i'm not going too quickly here all we're doing is defining a color variable this is equal to curses.color pair 1. so i'm just storing it in a variable it's the same thing as if i were to directly put it inside of here and then i'm saying if the character does not equal the correct character then we'll change the color from the default green that i have right here to be red and then whatever the color variable is that's what we're going to show for this character when we draw it on the screen and of course we're just comparing the correct character to the current character and we're getting the correct character by accessing the corresponding index in this string so let me get rid of these here and now let us try this so let's go back here let's run the code and let's hit enter and let's go hello and notice if i type something incorrectly now it is showing up in red but if i do it correctly then we are getting it in the correct color awesome okay so we are well on our way to completing this application we have a lot of what we needed done uh let me just have a look at my cheat sheet here and see what we need to do next all right so the next thing that we need to do is actually handle what's going to happen when we complete the text we also need to handle showing the words per minute because that's kind of the whole point of this typing speed test so i'm going to run the code one more time and show you that if i type more characters than in this text we're going to get an error notice that we get a crash here and it says index out of range and the reason why we're getting this problem here is because we have more characters in our current text than we do in our target text which means when we try to access target at index i we get a list index out of range because we're trying to access an element in the target that doesn't exist again that's because we have more characters in our list in our current text than we do in the target so when i'm on like the 20th element of my current text here and say my target text is only 15 elements long then i'm going to get a problem right i mean that's kind of a bad example but you get the idea that's what's going wrong so we need to make sure that once we get to the point where we've typed in all of the text or we've typed in the same number of characters as the target text we stop and we don't let them type anymore so what i'm going to do is after i look at the key here and after i check if it's a backspace i'm going to put an lf i'm going to say elif the len of our current text this will tell us how many elements are in the list is less than the len of our target text so this will make sure that we cannot add any more stuff to our current text unless there is room to add so unless the current text is less than the length of the target text we are not going to allow you to add another key even if they're the same we're still not going to allow you to add anything else to this because if you did then that would cause the index out of bounds exception so now let's try this and see if we're still getting that problem or not so let me run the code i'm going to begin and let's just go hello world this is some test text for this app i don't really care if i spell it correctly and notice here i'm hitting a bunch of keys but nothing's happening because we are already at the length of this text awesome okay so let's escape from that all right so now that we've done that i want to start showing the words per minute so we actually need to calculate what the person's word per minute is and then we need to display them now first let's just display it and then we can figure out how to calculate so notice i'm taking in wpm here so what i'm going to do is make a variable in my wpm test function and say wpm is equal to zero so that's what it will be equal to to start then i will pass wpm like that and inside of here we can just display it so i'm going to add to this string i'm going to say std.scr dot add underscore string such a mouthful here i'm going to say in an f string explain this in a second wpm and then instead of curly braces wpm and i'm going to put its location at one comma zero so that we go one line below where our target text is so what this is doing this f string is it allows us to actually embed python expressions directly inside of a string without having to add two strings together so usually you'd have to concatenate the strings but this is a little bit nicer you can use this in any version of python there's 3.6 and above to do a lowercase or uppercase f followed by just any string double or single quotation marks doesn't matter then you can write any regular string stuff that you would and if you put curly braces like this you can actually put a variable directly inside python will evaluate this as a string and then you will display that so i'm going to say wpm equals and then just wpm and that's all we need for right now so if we run the code so let's run this and i hit enter notice now we have wpm which is on the line below so now we just need to calculate the wpm and once we do that we will be all good okay so let's figure out how to calculate that so to calculate our words per minute we need to know how much time has a lap so far in the program we can't calculate our words per minute if we don't know how long we've been typing for so i'm going to go up to the top of my program here i'm going to import a module called time now time's going to allow me to actually time how long i've been typing for so i'm going to go inside of my wpm test function here and i'm going to start by putting a start time variable which is equal to time dot time now what this is going to store is a very large number that represents the number of seconds past what's known as an epoch now you don't actually have to really understand this at all the point is that this is going to tell us what kind of the current time is when we started doing this so right before we hit the while loop we'll record what our starting time was and then inside of the while loop every single iteration will calculate the wpm based on the start time so you'll see what i mean here but again just kind of keep keep in mind that what this is doing is keeping track of the start time it's a very very large number that's going to be the number of seconds past a certain date i think it's some time in like 1970 or something it doesn't really matter too much okay so we have the start time and what i'm going to say is time elapsed is equal to and then i'm going to get the current time minus the start time so the reason why this works is because time.time as i said is some like really large number that is the number of seconds past a certain date so if i call time.time again this is now going to give me the new time now this time is going to be larger than whatever the start time was so i take whatever the current time is subtract it from the start time and then that tells me how many seconds have elapsed and what i do need to do here though is use this min function or sorry use this max function and say the max of this and one you'll see why we need to do this but essentially this is going to result in us not getting a zero division error which we could get because what's going to happen is we're going to calculate the start time we're then going to go into the while loop we're going to immediately calculate the time elapsed and the very first time we do this the time elapse is going to be 0 seconds because the time between when we calculated this and this is going to be like nothing it's going to be the exact same time so this will give us 0 and when we have 0 when we perform the next operation that could give us a division by 0 error anyways now what we're going to do is calculate the wpm so the equation for wpm is a little bit complicated i'm just going to write it out then i'll kind of explain how it works so we're going to get the len of the current text underscore text like this we're going to divide this by the time elapsed divided by 60. now the idea here is that if we have say 30 characters typed so we have 30 characters and we typed them in 30 seconds then that means that our words per minute sorry our characters per minute would be 60 right if we typed 30 characters in 30 seconds and we're trying to figure out how many characters we're going to type in the minute then we need to multiply our 30 characters by 2. the reason we need to multiply it by 2 is because 30 is half of 60. now let's do the same thing except with a different time so let's say we type 30 characters in 15 seconds well if we did that then our words per minute is going to be 120. the reason why we know that is because we can take our time elapsed so 15 we can divide that by 60 and that's going to give us 0.25 so we know we're kind of at a quarter of an hour so now if we take 30 so let's take 30 and we divide this by 0.25 what this is going to give us is 120. now you can do this with multiplication as well i can say 30 multiplied by and then rather than 0.25 i would multiply it by 4. and then the way i would get that is i would reverse this operation and i would do 60 over 15. the reason it's 60 is because we have 60 seconds in a minute so that's kind of the rationale here but this is only giving us our characters per minute so this right here is characters per minute we're going to get the len of our current text which is how many characters we've typed we're going to divide that by how much time has elapsed over 60 and that's the characters per minute now it turns out that the characters per minute divided by five is the words per minute so we're assuming the average word has five characters so what i'm going to do is take all this and now divide this by five and now this is the words per minute so the characters per minute divided by five gives us words per minute now one thing i'm going to do here continues to get complicated is i'm just going to round all of this and the reason i'm going to round it is so we don't get some crazy decimal numbers we just get the kind of flat words per minute right i'm sure that makes sense but anyways that is how you do wpm now again the reason why we have this max of one and then whatever the elapsed time is is so that if this is zero this will just give us one right if it's anything less than one then it gives us one so that way when i try to do my division so i try to divide the length of current text by the time elapsed over 60 this isn't zero because if that was zero then i would get the zero division error okay so now we have the wpm and this will be calculated every single time we hit a key now i'll show you here how this works so let's run the code and let's see what our wpm is going to be okay so notice as soon as i start typing then the wpm will be calculated however right now i'm not typing and the wpm is staying the same the thing is it really should be decreasing right but the reason it's not decreasing is because let me escape out of here this line in our code key equals stdscr.getkey is actually what's known as blocking now blocking essentially means that we're going to wait here we're not going to do anything until the user types a key we're going to wait for them to enter key that's what this line is doing it's very similar to the input function in python right now the thing is i don't necessarily want that to happen if the user doesn't type anything i still want to show the wpm i still want it to decrease and i still want to show the text on the screen so what i need to do is actually call this one method here that makes this call no longer block now what i'm going to do is say stdscr dot and then i think this is node delay and i pass true inside of here now what this essentially says is do not delay waiting for a user to hit a key the problem is though now if the user doesn't enter a key this line throws an exception now an exception is an error in python we've seen them so the way that we need to handle this is we need to say try like this we need to say accept like this and then we need to say continue so what this is going to do now is make sure that this line won't crash on us if the user doesn't type something in and then if it does crash on us we go into this accept statement and inside the accept we say continue now what continue does is just bring us back to the top of the while loop it's just going to skip all of this right here because if this line caused an exception then this key variable it doesn't have a value right just it doesn't work we don't have a key because you didn't type in any key so there's no way for me to check what key you typed in if you didn't actually type in a key or you didn't hit a key so we need to skip the rest of this if we didn't hit a key hopefully that makes a bit of sense let's go through an example and i will show you how this works okay so let me go python tutorial.pi let's hit enter and let's start typing and again we're seeing that it's actually not updating the wpm so let me have a look here and see what's going on alright so it turns out i just need to save the code and rerun and now notice when i do this everything is working right the wpm is going down now let's just change our code a bit just so i can show you kind of what was going to happen if we didn't implement this try and accept so let's get rid of the try and accept and let's now just have key equals this remember we have this std scr dot no delay which means we're not going to wait we're just going to continue right so now watch what happens when i run the code i'm going to hit enter and immediately my program is going to crash we get this no input error because this line here right get key didn't actually get a key so that's why i did the try and accept i just want to show you that so that that explanation hopefully made a bit more sense okay so now we're getting the wpm now we're able to type in the text we're pretty much finished except what we need to do is we need to figure out what's going to happen when you actually type in all of the text right because at this point in time when we type in all the text nothing actually happens so we need to make sure that the user when they get to the end of the text they've typed in all of the characters correctly and if they have then what we'll do is tell them hey you know you won whatever and then we'll prompt them to play again so what i'm going to do is right after this i'm just going to check if the user's current text is equal to whatever the target text is the only thing is the current text is a list and the target text is a string so what i need to do is convert this list to a string to check if it is the target text now there's a really easy way to do this in python when you have a list and you want to kind of combine it all together into a string you can use something called dot join now what dot join does is it takes a list as an argument so i'm going to pass to this my current text current underscore text like this and you call it on a string now what it's going to do is it's going to combine every single character from this list with this string right here as what's known as the delimiter or the separator so if i were to make this a space then what would happen when i run dot join is i would get say i typed hello world there i would get h e space l space l space o it would space them all out if i made this a hyphen then it would kind of combine them together with hyphens in between but if i just make this an empty string which is what i'm going to do then what it does is it just combines all the characters together because well it's combining them with an empty string so they're just getting all squished together hopefully that makes sense but that's what dot join does it just takes all the characters and combines them together with this as the separator very useful method when you want to convert a list to a string so i'm going to say if dot join current text is equal to the target text then we are done so what i'm going to do is i'm going to say std dot no delay false the reason i'm going to do this is because i want to show something on the screen and then wait for the user to hit a key before i continue so if i want to wait for them to hit a key i need to get rid of this no delay thing because if it's still no delaying then i'm going to get an error if i'm trying to wait for them to get a key okay then i'm just going to break so i'm going to say no delay false i'm going to break and what that's going to do is bring me outside of this while loop so now that i'm outside of this while loop what happens is this function is going to be done so it's going to bring me back to where this function was called and then out here i can actually show something on the screen and prompt them if they want to play again so i'm going to say std scr dot and let's go with add string like this we're going to add this on the second row down so really the third row because i'm putting 2 and i'm just going to say you completed the text exclamation point and then press any key to continue dot dot and then i will say this dot and then get key so we'll wait for the user to type in a key okay so let's run this now and let's see what's working or if it's working so python tutorial.pie okay hello world so let's go hello world this is some test if i could type properly for this app exclamation point and it says you complete the text press any key to continue great and then of course our wpm is 47 because that's what we ended up so we hit continue and then it's just going to exit the program now what i want to happen is i want it to actually bring us or prompt us to play again so rather than just doing this and then ending i'm actually going to put this in a while loop so that i can play as many times as i want so you'll see what i mean but let's go well true let's put all of this inside of here and then let's get the user's key and if they hit anything other than escape we're just going to have them play again so we're going to say key is equal to this and we'll say if the ordinal value of the key is equal to 27 so if they hit the escape key we will break so they will exit otherwise we'll just continue so we'll just continue with the while loop we'll call the wpm test function again which is just going to re-run all of this it will reset all of our variables for us and we will be good to go that's kind of the beauty of doing everything in functions now i don't need to have any weird logic to run this again i just re-run the function right i just call the function another time and we are good to go so let's run this now and see if it is working okay so let's hit enter and then we'll go hello world this is some test clearly i can't type under pressure here when i'm on the recording app exclamation point okay 59 that's not horrible you complete the text press any key to continue we hit enter and what happened it actually crashed the program uh maybe it's because i didn't save this ah that would be why i didn't save my code so let's rerun this again and try enter okay hello world this is some test text for this i was doing so well app exclamation point okay we hit enter and then it brings us right back in perfect that is exactly what i want let me hit escape and then i can quit okay so that is pretty much all we need for the functioning application the last thing i want to show you is how we can randomize the text that's on the screen because it's going to get pretty boring if we just always have the exact same text right that's probably not the best so what i've done here is i've created a file called text.txt now you can do this as well i trust you guys can create a text file on your own you can probably right click on your desktop or whatever and make a new file and what i've done is i've put some different text here on different lines so notice every single line has kind of a different piece of text that i want the user to be able to type and i can add as many of these as i would like so what i'm going to do is read in this text file store all of these pieces of text and then randomly select one of them for the user to be typing when they're going with this program so i'll show you how we can do that we're going to write a function here i'm going to say load underscore text okay and all this is going to do is it's going to open up this file it's going to get all the lines and it's going to randomly choose one now since we're doing something random we need to import the random module so i'm going to say import random like that and we are now going to load the file so to load a file is pretty easy you can say with open then going to put the name of the file i'm going to go text.txt and i'm going to put the mode i want to open it in for now it's just going to be r if you want to create a new file you would do w so that actually is right mode so you would open a new file in write mode and you could write to the file i'm not going to show you how to do that but i just wanted to mention that but we're going to open in read mode i'm going to say as f and then what i'm going to do is i'm going to read all the lines so i'm going to say lines equal to f dot read lines now what this does this with is essentially ensure that this file will be closed after we open it this is known as a context manager it's pretty advanced i don't want to explain it too much but we say with and then we have this open function this is just how you open a file and we say as f which means we're going to take this kind of file object here this file variable we're going to store it in f and then we can access the file by using f inside of this width so just like the if statement we have our indentation right so i'm going to say lines is equal to f.readlines this is going to give me a list containing all of the lines of this open file and then i want to randomly select one of those lines and return it so i'm going to say return random dot choice on lines and then dot strip now what random dot choice does is just randomly choose one element from a list so if i have a list with 100 different things inside of it just randomly picks one and just gives it to me so it's just going to give me one of these kind of strings of text one of these lines now the thing is all of these lines of text have an invisible character at the end which is backslash n now we looked at this this is the newline character the reason why they have this is to tell my editor to display the file in this way if they didn't have the backslash n i would have no way to know kind of where my line breaks are in the the text file so what i need to do is i need to remove this backslash n which is what dot strip does from all of these lines because i don't want the user to have to type in backslash n i just want to remove it right so i'm going to say dot strip dot strip will just get rid of any leading or trailing white space characters and a white space character is the backslash n character so that's why i have dot strip okay so now we have load text this will just give us some target text so rather than manually coding in my target text now i'm just going to say load text and this will return to me one of the lines from this file now it's very important that this file is in the same directory as where your python file is if you put this not in the same directory it's not going to work it just needs to be in the same directory so you can see my tutorial and text.txt are right beside you now if you don't want to do this that's totally fine you can just manually hard code in the text but i figured i'd show this to you because it is kind of useful okay so that is pretty much gonna wrap it all up let's do a few tests here and make sure i haven't messed anything up too badly and then we'll wrap up the video so let's hit any key to begin and let's go subscribe so this looks like it's working to tech with tim on youtube clearly not for typing lessons okay perfect 43 solid i'm sure many of you can type a lot faster than that but let's go on to the next one and then notice it gives me a different string of text we can escape let me just run it again and okay it's giving me another test i'm just going to keep running this to make sure it's going through all of them and awesome okay so it does give me all of the different options that i have of course you can add more to this text file if you like so with that said that is going to wrap up the video there you go we have our wpm tester and i've showed you how we can add all kinds of styles and nice stuff to our terminal i thought this was a really fun beginner project i do kind of admit it's a little bit harder than maybe what i would consider beginner but i still think you guys were probably able to follow along at least i hope you were let me know in the comments down below and this showed you a ton of cool stuff that now you can add to your own projects if you guys have any ideas for what you'd like to see on this channel in the future please do let me know i'm always open to listening to your different project ideas or tutorial series or whatever you want in anyways i hope you all enjoyed if you did make sure to leave a like subscribe to the channel i will see you in another youtube video [Music]
Info
Channel: Tech With Tim
Views: 38,554
Rating: undefined out of 5
Keywords: tech with tim, python project, best python project, python for beginners, coding for beginners, typing test program, typing program, python typing test, tim typing speed, tim typing project, best coding project for beginners, tutorial for python, tim tutorial, curses module, curses module setup, coding typing WPM, typing speed program, tech with tim typing project, easy coding for beginners, python typing project, tech with tim programming tutorial
Id: NQ5i1kJAA6Y
Channel Id: undefined
Length: 59min 39sec (3579 seconds)
Published: Wed Nov 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.