3 Mini Python Projects - For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're a beginner or intermediate python programmer and you're looking for some short fun yet interesting projects to work on then this video is for you what I'll be doing is showing you three unique python projects and I'm going to walk you through the entire process from the beginning to the end that means that I'm going to be coding these completely from scratch I haven't attempted them I haven't looked at them yet and you're going to be able to see my entire thought process how I break down the problem how I attempt to come up with the solution and how I deal with any problems or bugs that occur in my program hopefully you guys will find that valuable and it will make it a lot easier for you to go work on your own projects or to extend these afterwards now one of the projects we're going to build well they will be time stamped down below but the first one is a pig game now this is kind of a dice rolling game where you roll until you get a one and then the next person gets to go then we're gonna work on a Mad Libs generator this means we're gonna have a story with a bunch of words that we're going to ask the user to fill in and the last one will be kind of a timed math quiz where we're going to ask a series of random math question and the user will have to answer them we're going to time them and see what their accuracy was with that said let's go ahead and get started and work on these three unique python projects alright so let's begin here right now I'm inside of Visual Studio code and I've opened up a directory that contains some different python files project 1 Project 2 and project three now you can write your python code wherever you want just make sure you know how to run the code and you can kind of interact with the editor and all of that so I'm sure some of you may be using idle which is the basic python terminal that's completely fine you can write your code there or you can do it in vs code like I have now I'm using python version 3.9 you can use any version you'd like and I'm just going to mention that I am going to assume a little bit of knowledge of python so I know that a lot of you are beginners you may not have even ridden python before but just in the sake of time I'm going to assume you know what a variable is you know print statements inputs for Loops kind of that basic surface level syntax although I will of course explain everything that I'm doing all right let's start with project number one here which is pig I also want to quickly mention I do have a programming course it's called programmingexpert.io if you guys like this style of teaching check it out from the link in the description and you can use discount code Tim the Big Value it has a ton of projects a ton of practice questions and over a hundred unique videos teaching python go Etc so we want to build Pig now first we need to understand what this project is so how does pig work well it is a multiplayer game first of all everyone plays a little bit different but the way that I play is that you essentially get a turn so comes your turn and you get to roll the die when you roll the die you're going to get some number from one to six if you get anything other than one you take that and you add that to the score of your turn so let's say you get a five now you're at five roll it again you get another five you're at ten and you can decide how many times you want to roll the dice now the catch is as soon as you hit a one whatever you got on your roll is gonna be done whatever you got on your turn is is zero like you completely remove the score so you can roll five times three times four times ten times twenty times as much as you want but every additional role you're gambling the possibility of losing your score score so you need to decide when you want to stop as soon as you stop whatever your current score is assuming it's not zero is going to get added to your total score and then you can determine the number of total rolls you want to have or it's whoever reaches like a hundred first or 51st whatever you want to do in our case we'll say like the max score is something like 50 whoever hits 50 First is just the winner of the game okay so how are we going to build this out well let's break down the individual things that we need to do first we need to allow the user to roll the dice this means we need to come up with a way to roll so we have to have some random number generator giving us a number between one and six we then need to ask the user if they want to continue to roll so either roll again or stop their track if they stop their turn we need to take whatever their score is and add that to some total score then we need to constantly check if either player has a score that is above 50 if it is above 50 then we need to end the game and tell someone that they won there's a few other things that are involved but those are the basic steps let's start by generating a random number so what I'm going to do is import the random module now random allows you to Generate random numbers what I'm going to do is just Define a rule function now a function in Python is a reusable block of code you make it using def standing for Define you then put the name of the function and then a set of parentheses we're then going to put inside of here in an indented block after a colon so notice some kind of one tab over any code we want to run whenever we call the function so what I'll do here is I'll say Min or Min value is equal to one max value is equal to 6 and then I'm going to roll the die in between them now there's a few different ways to do this with the random module but I believe we can say roll is equal to and this is going to be random dot Rand and then int like that and then this is going to be the Min value and the maximum value of our die then we're simply going to return Rule now the way the return keyword works is it's going to give whoever called the function this value kind of back it's going to return it to them so if I come down here and I say value is equal to roll the way I call the function is I use my set of parentheses and then I can simply print out the value here and you'll see that if I bring my terminal up and we run the code we get the value three we can run it again we're getting six three six five five two let's see if we can get a one okay it looks like it really doesn't want to give us a one this would have been a fantastic turn okay and there you go we got a one so that would have ended our turn all right there you go that is how we generate a random role okay so now that we have that we want to kind of set up the game and we might Begin by asking how many players are going to be participating and then we can keep track of all the different players maybe we set it to say a maximum of four or something like that so let's go down here I'm just going to zoom out a little bit and let's set up a while loop what we say actually not well we're going to say input so we'll say players is equal to input we're going to say enter the number of players and we'll say kind of one to four like that and then notice I'm putting a space here at the end just so we have a little bit of padding between where the user is going to give us the answer so the idea is we're going to ask the user okay give us some value between one to four then what we need to do is we need to check if this is a valid number so I'm actually going to kind of put a while loop here and I'm going to say while true and then I'm going to put this inside the reason for this is I want to continue to ask the user to input a number until they give me one that's valid so I say while true and then I'm going to break out if they give me a valid number so I'm going to say try or actually we can do the following we can say if players Dot is digit now is digit is going to tell us if this is a digit or not so if it's a number if it's a valid whole number specifically is what it will give us so I'm going to say if players is digit I'm going to say players is equal to int and then players now what this does is convert what's going to be a string here into an integer and the reason I need to check if it's a number first is because if I try to convert a string into an integer and it's not a valid integer it's going to give me an error so I don't want to crash the program so I'm saying okay if this is actually a valid number let's convert it into that number the reason I need to do that is I now need to check if this number is greater than 1 and less than 4. if it is I can then break out of this while loop because I have a valid number of players otherwise I need to continue to keep asking them to enter a valid player so I'm going to say if 1 less than or equal to players less than or equal to four okay then I will simply break what the break keyword will do is immediately exit outside of this while loop meaning we'll have access to the player's variable down here and we can just print out what the number of players is if we don't encounter this break keyword we will continue to ask the user until they give us a valid number of players now we could give them some output and say something like print invalid try again and we could do something here where we say something like prints must be between one to four players and actually it can't even be a one sb2 right so we'll have two to four like that and we'll put a two and we'll put a two because we need to have at least two players to be able to play the game all right so let's try this out let's run our code enter the number of players let's enter one says we need to do it again okay so it didn't actually print anything out um I don't know why that happened oh sorry it's because okay I'll fix that in one second that's an error anyways we enter two it says must be between two to four players which is a mistake and then two so notice I made a bit of an error because I put this inside of the if statement when I wanted to put an else statement here and then put this here because if this is valid we break which means we exit the while loop otherwise we tell them okay that was the error so we just need to make that quick fix there with the if statement okay in case you're confused by the way if statement checks if something is true if it is we kind of enter in this indented block which happens after the colon otherwise we have this else statement which will trigger if the uh if statement is false okay so I'm going to clear and run and let's go one okay it must be between two and four let's go five let's go hello let's go three and we're good to go okay so now we have the number of players and what we want to do is essentially simulate each player's turn where we ask them you know do you want to roll the dice yes or no they tell us yes or no they roll and then we start kind of calculating their score so let's do this first of all let's make some variables here and let's say our Max score is equal to and then we can do something like we'll go with maybe yeah let's go with 50 for now we'd probably want to make that larger but just kind of for the sake of time when we're testing this we'll make it small then we want to have our player underscore scores and what we'll do for our player scores is we'll just have a list which contains all of the individual scores now we don't know if we're gonna have two players three players or four players so we're gonna have this list kind of change size based on the number of players that we have now the way that we can kind of create or initialize this list based on the number of players is we can say 0 4 underscore in and then something like this in range the Len of players now what this is is actually a list comprehension I was thinking of the name there what this does is it essentially puts a zero inside of the list for every single player that we have so what I'm doing is saying for underscore now I could put I here but the thing is I don't actually care about what the variable is I just want to do this the number of player times so I can put an underscore essentially saying hey I don't really care what the variable is so like you know we need to have something there but I'm just going to put an underscore because I don't really want to use this and then I'm going to say in range the Lan of players so what this range function will do is Loop the number of players with the length of actually sorry we don't need Len just players here it will Loop the number of players times that we have notice that our players is going to be an INT right so if it's three this will happen three times if it's four it will happen four times Etc ignore the Len that I had there I don't know why I put that there just a silly mistake okay so now we can print out our player scores and you'll see that we'll get an array filled with zeros based on number of players that we have so if we do three gives us three zeros if we run it again four we get four zeros so now we are able to store each of our players scores inside of this array fantastic okay now we need to go through our player terms so we're essentially going to say wow the maximum of our player scores is less than the max score then we'll keep looping now what Max will do is give us the maximum value from this array or from this list so if it's 23 gives us 23 if it's 55 gives us that we're saying okay as long as it's less than the maximum score means no one's reached the max we'll keep going as soon as someone does reach the maximum score we need to stop and then tell the user whatever the um sorry tell the players whoever kind of won the game all right how we gonna do this uh well let's go here and we will say first of all roll we'll say should underscore rule is equal to input and we'll say would you like to roll question mark and we're just going to put here A Y which means we want the user to input the key y so then we can come here we can say if should roll dot lower is equal to Y the reason I'm converting this to lowercase which is what dot lower does is because if they enter a capital Y I still want them to roll right so this is just checking is it capital or lowercase doesn't matter we're going to convert it to lowercase and then check if it's equal to the lowercase y if it is then we're going to roll the dice for them so we're going to say roll is equal to and actually I can't use roll because let's name my function so I'm going to say value is equal to roll okay otherwise I'm simply going to break and actually a better way to do this is going to be a little bit cleaner for me is I'm going to say if should roll.lower does not equal y then I will break out otherwise I will say my value is equal to the rule so the reason I'm doing this is because I want to have all my code kind of not indented if I don't have to have it indented so I'm saying okay if you didn't hit Y that means we're just going to stop your turn otherwise we're going to roll the dice now there's a lot more code we're going to need to write but I'm just kind of going through this um you know as I normally would and then we'll kind of stop and fix things since we need to so we're going to grab that rule and we're going to check if that rule is equal to one if it is equal to one then whenever the player's current score is is just going to become zero and we're going to move to the next person's turn now we still need to simulate the terms and all of that which we'll do in a minute but for now just kind of bear with me so we're going to say if value is equal to zero then we can simply say print or sorry not zero I'm going to say you rolled a one turn done exclamation point otherwise we'll print you rolled a and then whatever the rule is so we can say something like actually value like that okay so let's put a colon U rolled uh and then whatever the value is all right so now that we have this we're kind of printing out what they ruled I'm just going to go up here and I'm going to Mark the current score equal to zero because we need something to keep track of what the player's score is now if they didn't roll a one then what I'm going to do is I'm going to say the current score plus equals whatever the value of the die was I'm going to tell them what they rolled and then down here I'm just going to print your current or your score is and then we'll print out whatever the current score is and then we'll kind of continue now here I'm just going to break this because the idea is all right well if you roll to one your turn's done so we're gonna break out all right so this kind of simulates ones per one person's turn story but again we haven't really kind of handled uh the fact that we have multiple people going at once and allowing someone's turn to continue multiple times so what we actually need to do is we need to use a for Loop here what we'll do is we'll put a for Loop kind of right here and we'll say four I in range players so I is going to represent the current player so we'll say player underscore index or player idx so player zero one two three or player one two three four whatever you want to call it and in this for Loop we'll then simulate one person's entire term so we'll mark the current score but then all of the stuff that's going on here needs to happen until the player stops their turn so what we'll do is we'll have a wall Loop and we'll say while true and then we'll put all of this here then outside of the while loop we're going to say that the player underscore scores at index player idx is going to be plus equal to whatever their current score is so like the current score is kind of the score per turn and then the total score is what we're going to be adding to after they finish their turn so what we'll do after we add that is we'll say print your total score is colon and then we will say that this is going to be the player scores at the player idx okay we'll go through this again in one second but for now I we're making good progress on the terms all right so I'm just reading through this uh it's looking okay however I'm realizing that if the value is 1 then we're going to have to set the current score equal to zero just so we make sure we don't add what their score was before they rolled a one all right so let's kind of run through this here we have our for Loop we say four player index in range players which means We're looping over all of our different players so they each get a turn we probably want to print out something like player and then let's say player idx plus one turn has just started just they know that it's now their turn and what I'm going to do is print a backslash n right here and a backslash n what this is going to do is just add a line break so that we have a bit of Separation when the player's turn starts so we say okay player and then we're doing plus one because this is going to be zero one two three well we probably want to show one two three four so that's what we're showing so we say player whatever it is turn has just started we can say player number turn has just started okay we said the current score equal to zero for the turn we simulate the turn we ask them if they'd like to roll if they say yes or if they don't say yes sorry we break out otherwise we rule if they get a one we say they roll to one their turn is done their current score is zero we break out otherwise we take whatever the current score is and we add to that the value that they rolled because it wasn't a one and then we continue this whole process again so we tell them this is what you rolled and then we say your current score is this which is going to be the sum of what they rolled plus you know what their current score is okay then eventually we're going to get outside of this while loop when they roll one or when they tell us that they don't want to roll anymore we're going to take that score add that to their total score and then print that out now we're not quite finished but this will simulate most of the game so far all we really need to do is determine when someone wins so let's zoom out a bit just so we can kind of read most of what we have here we have our role function we ask the player or we asked to input the number of players we want we have the maximum score generate kind of an empty list for storing the scores and then we have this code which is going to simulate the terms let's run this and see what we get here okay so let's zoom in a bit enter the number of players let's go with two player number one turn has started okay would you like to roll yes you roll the six your score is six would you like to roll yes you rule to four your score is 10 would you like to roll okay yes let's keep rolling yes yes yes yes I'm trying to get a one here and it says you roll to one your turn is done yours total score is zero player two staring at sorry would you like to roll yes five okay let's roll again and we roll the one all right and you'll see that we can continue this right so I'm gonna try to get a pretty high score although this isn't working too well for me okay let's stop rolling there now our total score is 15 player two Stern okay yes yes yes yes yes yes yes really gambling here okay let's stop the roll okay yes roll to six your score is six yes yes yes yes yes no okay total score 42 let's roll roll roll and no total score is 51 and then notice the program ends here because all of the players turns have finished and someone had a score of 51. so we end we should really be telling them you know that you won which is what we're gonna do now but that kind of simulates the program there's a few things we'd probably want to do to make this a bit better for example at the beginning of the turn we should probably tell them what their score is so that they know that before they go into the turn so they can stop um you know once they hit what the max score is okay so let's do this uh we're gonna say player one's turn has just started and then we're going to print here your total score is colon and then the player scores at the player idx okay and then we're going to begin the turn so we can actually just remove that backslash n and we'll just put one right here just so that we kind of go one line down okay do all of this and then as soon as this ends what that tells us is that at least one player has a score above 50. now multiple players can have a square above 50 and the reason this works is as soon as someone hits 50 we don't end until every player gets to turn the reason why that's fair is because whoever goes first would have an advantage so in this case we let everyone have at least that last Rule and then we see whoever has the max score after the last roll so what we can do now is we can just find what that maximum score is we can actually get the index of that score inside of our player's scores and that will tell us which player won so the way we do this is we simply say Max score is equal to the max of our player's scores and then we're going to say player winning idx is equal to or actually we can just say winning idx is equal to the player's scores dot index and we can index the maximum score what that does is give us the index either 0 1 2 or 3 of where that maximum score is which tells us player zero player one player two Etc so then we can just print player comma winning index plus one so we'll say player number and the reason we're adding one is because again we're going to start at zero so we want to make sure we're going from one to four not zero to three and we'll say is the winner with a score of colon and then what the maximum score was okay so that should wrap up the game let's give this one last run here okay Enter the number of players let's go with two player number one's turn is just started your total score is zero would you like to roll yes roll to five let's roll again roll again roll again and no okay 16 would you like to roll yes okay that ended fast you like to roll yes yes yes yes yes no okay and Toes score is zero would you like to roll yes yes yes yes yes yes yes okay yes yes and no okay now it's player two's turn so let's roll yes okay that ended fast yes okay now we have the max score so let's say no player two still gets a turn even though we now have a score 51 I'm just gonna say no and it says player number one is the winner with a score of 51. okay so that completes that first project all of this code will be available from the link in the description by the way in case you want to download this it'll kind of scroll through it quickly so you can get a sense of how we wrote this that was Project one hopefully you found that interesting and useful now let's move on to project number two alright for project number two we're gonna do that Mad Libs generator the idea is we'll have some type of story this story will have replaceable words like an adjective or a location or a weather condition whatever it may be what we'll then do is we'll ask the user to give us all of the different words we'll then inject them in the story and then read the story back out to the user now the first thing we're going to need for this is a story so the process as I kind of described there is have a story with those injectable words essentially grab what those words are ask the user to input them and then place them in the story now I'm going to show you that there's a lot easier way to do what I'm about to do or I'm going to mention this a lot easier way to do what I'm going to do but I want to show you a way that we can do it with any story so that means that you can make this program really Dynamic and you could have like 20 stories 50 stories 100 stories or you can just replace them without having to write any additional code the way we're going to do that is by taking the story and placing it inside of a text file that way as long as the story is written in a specific format we can read in the story do the operations we need to and then kind of run the program as we see fit now what I always do whenever I need something like this is I don't write it from scratch because I'm a lazy programmer I go to chatgpt so what I actually did here is I asked it using this prompt I'm making a Madlibs generator in Python let's make this full screen I'm going to need a story where the user can give me some words can you give me the story or even though I spelled that correctly and for all of the words where the user needs to give them to me make them like this angle bracket word now it gave me a nice long story and it had all the different words and it kind of listed them all out that was a bit long for me so I said that's okay but can you make it in less words please and then it gave me uh kind of a shorter story so the idea here is that whenever we have these angle brackets we're going to kind of treat that as a word that needs to be replaced so what we'll do is we'll take this story from chat EBT so I'm just going to copy this on my clipboard we'll put it in a text file we'll load that story in for all of those individual words we'll then kind of grab them figure out what they are ask the user to give us a word for them and then replace them in the story all right how do we want to do this well the way I will do this is I will copy that first of all and I will go here to vs code I will make a file called story.txt and I will paste that inside of here now make sure when you do this that you put your story in the same directory where your python file is so here I have project 2. pi and I have story.txt in the same directory very important that you do that otherwise this isn't going to work for you okay and if you want this story you can go click the link in the description and just download the story from GitHub I'll make it available for you um after okay so how do we do this first thing we need to do is load this in now I want to load this in as a string essentially and then be able to kind of parse through that string and look for all of those different words now there's a few ways to do this but the way I will do this is I will say with open I'm going to open story.txt in read mode as F and I'm going to put a cooling now what I've done here is I've used the open function and the open function allows me to open a file so I can open a text file a Json file really any type of file I want and the mode here which is what I've placed second is the way in which we're kind of reading it in so I'm going to read it in in read mode which is R mode if I put W this stands for right mode where we'd actually be creating a new file and overriding a file if it already existed with the same name so we don't want to use W mode we want to use R mode this now allows me to read this story.txt file in and the reason I'm using this with syntax is what this does is provide something known as a context for this file which means I can do any file operations on this variable F inside of this indented block and as soon as I get outside of that indented block python is automatically going to clean up the file for me which means it will close it and make sure everything's good with the file it's kind of best practice in terms of opening files here so what I'm going to do is say my story is equal to F dot read now when I do dot read all this does is give me all of the text inside of the file so I can simply print out story here and you'll see if I run this that I get my story appearing here in my term okay so now that we've read that in what I would like to do is look for all of the words in my story now there's a bunch of different ways to do this but essentially what we need to do is find the presence of this angle bracket as soon as we find that we want to find up to this closing bracket right so we want to find every individual word take that word store that in some kind of list that we're going to have access to ask the user to give us a word to replace it with and then go and replace all of the instances of this word with whatever the user told us okay so let's do that what we want to do is a loop through our story now our string we can actually access all of the individual characters as an index so what I can do let's zoom in here I say for I comma character in enumerate and we're going to enumerate the story now what enumerate does is give us access to the position as well as the element at that position just makes it a little bit cleaner for us here so now I'm just going to check essentially if this is equal to an angle bracket if it is equal to an angle bracket then I kind of need to do something to Mark the fact that I found the start of a word and then I need to go until I find the end of it so we're going to set up a few variables here and we're going to say words is equal to this because we want to store what all of these different words are and then what should we do here we should say start of word is equal to negative one okay so we're going to have this variable and this variable is pretty much going to tell us if we've currently found the starting index of a word if we have that means we can then look for the end if we haven't then we're going to wait until we find that starting index so what we'll do here is we'll say if Char is equal to and I'm going to make another variable I'm going to call this Target underscore start this is going to be equal to the open angle bracket I'm going to have my target ends equal to the closing angle bracket just so that if we were to change the format of the story we could easily adjust this by changing the value of the variables so I'm going to say if the Char is equal to the Target start then what I'll do is I'll say the start of the word is equal to index I so if I found that opening angle bracket okay we just found the beginning of the word let's mark that by indicating that in our variable now what we'll do is we'll say if the Char is equal to the Target end and the start of word does not equal negative one the reason why we're doing this is we're saying if we found the ending angle bracket and we had the starting angle bracket right which will be marked by the start of word variable which if it's equal to negative one means we haven't yet found the beginning if it's equal to anything else that means we did find the beginning then we can take that entire word and add that to our words list so what we'll do now is we'll say word is equal to and this is going to be story at index start of word up to the index I plus 1. now what this gives us access to is a slice which is a subsection of the string so this is the slice operator here in Python where we indicate the starting character so the character we want to begin taking a slice from and then the ending character that we're going to go up to but not including or the ending index so in this case we know the start of the word so we put that here and we know the end of the word because that's the current index that we're on that we just found but we need to add one to it to make sure that we include this ending index in our word so we're going to grab that we're going to put that inside of our uh word or word variable sorry we're going to say words dot append word which means we're going to now add this word into our words list and then we're going to set our start of word equal to negative one because we've now found one whole word so now we reset the start of word so we're ready to handle it for the next uh character that we find which is that opening angle bracket so this code here will actually find all the different words for us in the story so let's test this by printing our words okay let's run the code and I think my terminal is a little bit cut off but you can see that we get all of the different words now what you might notice here is that we have duplicated words now the reason we have duplicated words is because this word could appear multiple different times now that's fine if it does but I only want to have the words that are unique now the way that we can essentially do this is we can change from using a list and instead we can use a set now a set is a data structure which will only contain unique elements so if I add the same element in it's not going to do anything we just have one instance of each unique element the way to do that is just to write set here instead of a list and then rather than append we're just going to say add now add we'll just add this to the set and you'll see now that when we run this we'll just get all of the different unique words perfect okay now that we have those words what we want to do is ask the user to give us a value for each of the words so the way we'll do this is we'll set up a dictionary so we're going to say answers is equal to a dictionary the idea is we want to have the word as the key so something like name and then the value in our dictionary will be whatever answer they gave us so something like temp the reason we'll do this is so we can very easily look up the value of a word so we say name Tim blah blah okay and then what we'll do is we'll take whatever these values are and replace them in our story later on the way the dictionary works is you add a key by doing something like this answer is key is equal to and then whatever the value you want and you can access the key by just using the square bracket syntax like this okay so we have our answers this is going to be empty dictionary what we'll now do is we'll Loop through all of the unique words that we have and we'll ask the user to give us a value for them so we're going to say forward inwards iterating through our set and we're going to say answer is equal to input enter a word for and then we're gonna do Plus and then whatever the word is now notice we can't do a comma here because we're using the input statement not the print statement so we need to concatenate which means add two strings together so we take the word add it to enter word four and then we can just put here a colon with a space at the end so that the user knows where to start writing okay so that gives us an answer we're then going to say answers at word is equal to answer and let's spell answers correctly okay that will now create a dictionary that has all the words associated with all of the values so let's now print out answers okay so let's come here it says enter a word for object uh let's go something like chair weather condition Sunny adverb uh I don't know let's see gently place School emotion two sad animal bear place to um I don't know house Terrain what is this grass okay character Tim adjective one jumps emotion happy and there you go it gives us now all of the different words and their answers now that we have that we can take every single instance of these words and essentially kind of replace them in the story so now we're gonna have another for Loop and we're going to say forward in words now the great thing in Python is that we have this method called replace all or replace I don't know which one is I think it's replace all either way what this is going to do is replace every single instance of one string with another so that's super convenient for us because we know this string we want to look at which is that kind of angle bracket and we want to replace that with our answer so now what we'll do is we'll simply say story dot replace I think this replaces all instances of it we're going to check that in one second and we're going to replace our word which is that angle bracket string with the answers of our word and then we can just print out our story and we'll be good we'll have the entire program finished so let me quickly run through the code and then I'll show you how this works so we open up our story.txt we read this in we create a set of all of our unique words we then say the start of the word is equal to negative one we have our Target uh kind of opening bracket Target closing bracket and this first block of code here goes and locates all of the different words that are inside of our story so we say for I character and enumerate story getting the index as well as the value we say if the character is equal to that starting kind of open angle bracket let's mark that by saying the start of word is equal to I then down here if we found the ending bracket and we have the start of a word we've just found the end of the word so now what we can do is grab that word using our slice syntax add that to our set and then reset start of word then we have all of our words so we need to generate an answer for them which we asked the user to do and then we simply go through and replace all of the instances of our word with the answer the user gave us awesome let's give this a shot let's make this full screen and run animal bear adverb gently character Tim I probably should do lowercase four but that's fine weather condition Sunny terrain grass place School adjective one um I don't know throw emotion two sad place two let's go with soccer field emotion happy object chair okay and unfortunately it seems as though this actually didn't work so I'm gonna have to look at what the problem was here ah I already know what the issue so I said story don't replace however what this does is this gives me a new string so what I was doing is I was just kind of generating a string that I wasn't storing anywhere so story didn't change this gives me a new string so what I actually need to do here is say story is equal to and then story dialer place and then do this now there's more efficient ways to go about doing this but this will be perfectly fine for our purposes so if we fix this up now and let's save that so again just we kind of are now changing the story every time by updating that variable what I'll do is run this again motion sad adjective throw character Tim place School terrain grass place two soccer field object chair adverb gently motion sad animal bear weather condition Sunny okay in the throw land of school oh adjective okay I I probably used the wrong thing for an adjective there anyways in the throw land of school a bear was feeling sad the bear had lost its chair suddenly a Tim appeared I will help you find the chair they said together they journeyed through grass and face the sunny okay I probably should have said like raining or something finally they found the chair in a soccer field the bear was so sad and thanked the Tim they lived gently Ever After all right you know what that's not so bad I think there was a few uh words that are probably messed up writing but there we go we get the story and yeah there you are now if you wanted to you could store that in a text file but I think that that pretty much wraps up this project that was pretty cool and obviously if you kind of change the story around give some better words you could get some pretty interesting results there regardless hope you guys enjoyed let's move to the next project alright so let's get into project three now the idea for project three is we want to randomly generate a bunch of different math questions ask the user those questions and then not let them continue until they get it correct well then time how long it takes them to answer all of the questions so the first thing we need to do is be able to randomly generate these problems now there's much different parameters we can use right we have a set of operators we can use it's like plus minus addition multiplication we can even use exponent and stuff if we wanted to and then we have kind of the max values as the Opera Ranch like the min max value Etc so what I'm going to do is write a few variables that we'll use as constants that we can change that will make this either easier or harder depending on what you want to do now first of all we're going to need the random because we're going to be randomly generating operands then we're going to need the operators so I'm actually going to do this in all capitals I'm going to say operators is equal to this and I'm going to say min operand and Max uprant okay we'll say for the Min maybe we'll have a Min value of three and for the max goes something like 12. for The Operators I'm going to use a string and I'm going to put in python operators okay it's important that these are ones that could actually be used in Python because I'm going to show you the way that we'll actually figure out what the answer is this is not going to be that long of a project but it'll show you a cool function that you might not have seen before in Python so what we're going to do is write a function called generate problem okay now what this is going to do is generate a problem for us that uses one of these operators and then a random operand on the left and right side so we're going to say left is equal to random dot Rand int and this is going to be the Min operand and the max operand we're then going to say right is equal to random dot Rand int and again the main operand and the max Opera now I'm actually going to remove the vision because I'm realizing that the way I'm going to write this is not going to work too well with division we need like a separate set of parameters so for now we'll just use these three operations which I think is fine okay we're then going to randomly select an operator so we're going to say operator is equal to random and there's actually this function called Choice what this does is just randomly pick one of the elements from a list so I can say random not Choice operators and that'll just give me randomly one of these then I'm going to say my expression which is XPR or expr here is going to be equal to the left Plus the right plus the operator however I need my left to be a string and I need my right to be a string okay so this is going to give us the expression which will be the left hand side plus the right hand side and what we'll actually do is we'll just add a space in between here so I can print this out and you can just see kind of what it looks like so let's add a space like that okay so now let's call generate problem and let's just print out the expression and then return it just so we can see what that is okay so clear and python project three and I got an error can only concatenate string not into to string okay ah sorry that's because I put this in the wrong order so operand or operator sorry plus the right okay so let's fix this and run again and we get 12 plus 9. let's run a few more times and you can see we're kind of getting random operands and random values sweet all right that's working now the question becomes okay we randomly generated this but we need to know what the answer is right since we're randomly generating it it becomes a bit more difficult to calculate the answer now what we could do is just use a bunch of if statements right and I can say if the operand is plus add them together if it's minus subtract them if it's multiplication multiply them that's fine however I want to make this a bit more Dynamic such that we can kind of make this a bit larger and we don't have to do all those if statements now the way to do that so we'd actually do this we can say answer is equal to a function called eval now what eval does is evaluate a string as if it was a python expression now notice we have an expression that is valid python code like 6 minus 12 is valid code that we can evaluate so all I have to do is put this expression inside of the evaluation and it actually just tells me what the answer is so now what I can do is I can return my expression and the answer like that and if I go here and say expr answer is equal to that I can print them out so I can print the expression excuse me there sorry but I can print the expression and the answer and will tell me what it is so I'm just showing you how we can kind of avoid having like if it's this do this otherwise do this that's fine that's valid to it to evaluate it that way but we can use this eval function so if I run this I get 7 minus 3 that's 4 right 12 minus 4 8 10 plus 5 15. so I figured you guys would find this eval function kind of cool hence why I wanted to show it to you alright so that is our generate problem and that's really most of the coding done what we need to do now is determine the total number of problems we then essentially need to set the timer when the user begins and we need to ask them to answer it right so what I'm going to do is say total problems is equal to 10. set this to whatever you want but the idea is I'm putting all of these capital or all capital variables at the top of my program these are constants meaning they're not going to change and we can simply change them around like once we're kind of going to run the program a second third time to make this more challenging easier like we can set the parameters of the program from these variables right up here okay so now that we have that what we'll do to begin is we'll kind of just ask them the number of problems then we'll set up the timer so we're going to say four I in range and then this will be the total problems and then we'll print something like this we're going to say expression answer is equal to generate problem so that gets us a unique problem we're then going to print problem number and then we'll say plus I plus 1 inside of parentheses and this will need to be a string so what we're doing is we're converting I plus 1 to a string so it'll be like problem one problem two problem three so they know then we're going to say plus a colon now notice the reason I'm using pluses here is because I don't want to have a space between my different values I just want to have them kind of inline so I'm saying problem number one colon space plus and then the expression okay and then we'll put actually I'm realizing we're going to put this inside of an input statement sorry so we're going to switch this around and we're going to say guess is equal to input and then we'll have plus the expression plus a equal sign like that so now what we're doing is we're saying okay problem number one here's the expression so here's the problem equals and then the user can type in their answer and then what we'll do is we'll keep asking them this until they get it correct sweet so then inside of here we'll do a while loop and we'll say while true we'll do this we'll say if guess is equal to answer then we will break now the thing is our answer is going to be an integer but our guess is going to be a string so we're going to convert r into a string so we can compare the same types because if you compare an INT to a string even if they're the same value like the string 10 the number 10 is going to say false now I want to convert the answer to a string not the guess to an INT because if I tried to convert the guess to an INT to match the answer type and they had typed in something that wasn't a valid int then that would crash my program which I don't want so this way I'm kind of solving for that I'm going to make sure that I don't have any potential crashes okay so if they get a right break otherwise we're just going to keep asking it to them we don't need to give them any feedback they can just keep attempting the answer okay so that's good and I think that's actually going to be it for all of our problems all right so let's run this now give this a shot now I'm about to embarrass myself I haven't done math in so long so let's hope it gives me some easy ones okay nine five five times eight forty okay 20 10 8 times 7 I always mess these ones up uh not 64.56 okay 88 and there we go we finish the problems all right so that was actually pretty good did I get any wrong no I didn't okay let's try it again let's get one wrong this time and no this is going to keep asking until we get this correct and then we continue I'm just gonna hit Ctrl C to exit out of that and now what we'll do is we'll set up the timer now I would love to make it so the timer like it shows you the timer it's gonna be a little bit difficult to do without going into some more advanced code so what we'll do is we'll just start the timer as soon as they say like they're ready to start and then we'll tell them what their time was at the end and we can also tell them like number of correct answers so let's do something like this we'll say wrong is equal to zero and every time they get it wrong so we can just go here and say wrong plus equals one and then that way we can tell them kind of what their accuracy was at the end all right so wrong we're also going to import time all right and now before we start doing all of this we just want to ask them like okay are you ready to begin as soon as they hit enter then we start the timer just they have a second to like gather themselves before it begins so I'm going to say input I'm going to say press enter to start okay really they can press anything they want and it's going to continue as long as they go to the next line so they can type something in if they want doesn't matter so press enter to start we'll print out like this okay and then it will begin and I'm just going to take this and print it once they're done and then we'll print nice work and then whatever else we want to do okay so as soon as we do this so as soon as they hit input we're going to start the time now I imported this time module and what I can do is I can say the start time is equal to time.time now this is going to give me what's known as the Unix time yeah I think it's that's what it's called let's see here so actually sorry it says return the current time in seconds since the epoch don't worry too much about exactly what that means but it's going to give us some kind of time stamp in seconds so what we do is we kind of Mark the start time then we just need to figure out what time they ended at and we subtract the two times and that tells us the total amount of time so as soon as they finish the for Loop here we say end time is equal to time.time and then we just say total time is equal to the end time minus the start time and then we can print out how long it took them so we'll do that down here nice work you finished in comma total time and we'll do comma seconds like that now we could parse this and do minutes hours Etc but I think just doing the number of seconds is fine and that will be our program okay let's give this a shot so run the code press enter to start okay so that's 80 that is 17 negative 2. negative 6 11 77 fortunately it's giving me some easy ones here 16 negative 3 36. okay nice work you finished in 16.95 blah blah whatever seconds we could probably round this off if we wanted to to make it a little bit better so let's do that and we will take this and round this to the nearest two digits okay let's do this one more time see if we can beat our time of 16 seconds that was pretty fast I don't know if I'm gonna be able to do that Okay negative two negative one negative one oh what am I think one okay definitely not gonna win Now 49 14 30. negative 4 12 0 10 21 17 oh I was pretty close even with that mistake I was reading it as 7 minus eight not eight minus seven hence why I made the mistake three times anyways that was pretty good all right so that completes our program and all of our projects now I realize I kind of ended up doing the projects in the reverse difficulty order where the first one was the hardest second was medium last one was the easiest anyways I actually had a lot of fun building these projects like I said I haven't done any of them before I hope that you guys enjoyed this video as well and uh kind of got the taste for how I write code how I come up with the solution how I break this down to be honest this is relatively simple for me a lot of this you know doesn't require a ton of thought but I hope for you guys that it was valuable and you can kind of see how you even make variable names how you structure things into functions I could clean this code up and make it a lot better but I wanted to kind of you know blur the line between you know what could be expected of a more beginner programmer and what are some better practices when it comes to coding if you guys want any of the code it will be available from the link in the description let me know if you enjoyed these projects if you extend them if you're working on some other cool projects always love to see your comments again if you guys want to check it out I have that programming course programmingexpert.io that'll be available from the link in the description if you enjoyed make sure to leave a like subscribe the channel and I will see you in the next one foreign [Music]
Info
Channel: Tech With Tim
Views: 344,774
Rating: undefined out of 5
Keywords: tech with tim, python for beginners, python tutorial for beginners, python projects, python programming language, python programming, learn python programming, python course, python crash course, python from scratch, python tutorial, python tips, python full course, python ideas, python basics, unit converter, what is python, short video on python, projects, python language, getting started with python, project, software engineer, learn python, shorts, python
Id: 21FnnGKSRZo
Channel Id: undefined
Length: 53min 53sec (3233 seconds)
Published: Mon Jul 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.