5 Mini Python Projects - For Beginners

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 five python mini projects for complete beginners now each of these projects shouldn't take us longer than about 15 or 20 minutes to actually code out what i'm going to be doing is giving you kind of a starting template for these so i'm not starting with any code pre-written or anything like that in fact everything i'm doing here is completely from scratch i don't have any code on my other monitors or anything like that and the idea behind this is i'm going to show you my kind of thought process in creating these games explain to you why i've written the code that i did and kind of how it works and then that should hopefully give you guys a good idea how to go about extending these games and kind of customizing them to your liking now as i said these are for complete beginners when i say complete beginners i'm kind of referencing people that have a little bit of familiarity with python have maybe looked at the syntax a bit but aren't quite comfortable yet are still considering themselves beginners and kind of want some projects to work on and some i guess things that they can use to actually apply their skills rather than just looking at straight theoretical tutorials so anyways with that said i hope you guys are looking forward to this let's get into it after a quick word from our sponsor before we get started i need to thank the sponsor of this video which is alco expert algo expert is the best platform to use for preparing for your software engineering coding interviews and has the highest quality coding interview practice questions with 155 practice questions detailed solutions in nine of the most popular programming languages a feature-packed browser-based coding environment extensive test suites and conceptual overviews and code walkthroughs for each and every problem algo expert is the best resource to use to ace your coding interviews algo expert also has a data structures crash course coding interview assessments and a mock interviews feature i can highly recommend algo expert as a former customer myself and now an official instructor on the platform get started using algo expert today by clicking the link in the description and using the code tech with tim for a discount on the platform alright so the five projects i have for you and in the order in which i'm gonna be showing you them is a quiz game a number guesser game rock paper scissors choose your own adventure and then finally a password manager now these are going in the order of difficulty so the first one i'm showing you is the simplest the last one i'm showing you will be the hardest but the last one is still not going to be that hard if you're a complete beginner you should still be able to actually code it out and understand what's going on now if you guys want to skip to a certain project you don't want to go through one of them whatever there will be some timestamps in the description and kind of chapters on the video timeline so feel free to skip through to whatever you want and now what i'm going to do is quickly show us how we can set up our environment and then we'll get started working on this first project which is the quiz game all right so right now i'm using something called visual studio code this is known as an ide an integrated development environment you totally do not need to use this if you have this installed then you know how it works go for it but for most of you i'm going to recommend you something called idli so when you install python i'm going to assume you have python installed at this point in time what you're going to do is open up this app called idle if you're on windows you can search for it on the windows bar if you're on mac you can look for it in the spotlight search which is in the top right hand corner so open up idle again this is installed by default when you installed the kind of vanilla version of python and this is going to bring up something known as the interactive shell now this is not where you write your code i want to make that very clear a lot of people make the mistake of writing all their code here no don't do that what you want to do if you're using this is go to file press a new file and now you have the place you're actually going to be writing your code so the first thing i recommend you do is save this file so save it in a folder save it on your desktop wherever and name it something right name it like the name of the project quiz game uh choose your own adventure game whatever i would recommend you make new files for every one of the new projects that you are working on and so i'll just give give you an example here i'm going to save this as test.pi and now if i go in here and i type you know print hello just some basic python syntax the way i actually run this code here is i press run and then run module or alternatively i press the f5 key and then notice it's going to bring up this interactive shell it's going to print out and run your program and then you can go back here modify it save it and whenever you want to run it you press f5 or press run and then run module all right so hopefully that gives you gives you guys an idea how to set up your environment obviously feel free to download visual studio code as well leave a link to it in the description or use any coding environment that you're comfortable with and that you know how to use so right now have a python file open this is a file that's on my desktop it doesn't really matter where you put the file and the first thing i'm going to do here is start working on the quiz game so this quiz game the idea behind it is we want to ask the users a bunch of questions and then if they give us the right answer to these questions we'll kind of add one to their score then at the end of the program we'll print out what they got out of the number of questions so if there was 10 questions we would say hey you got you know 3 out of 10 or 7 out of 10 or whatever and maybe we'll even give them a percentage or something like that so let's get started the first thing i'm going to do here is use the print command or the print function i'm going to assume you guys are familiar with this but when you want to print something to the screen output something to the console you say print you put some value in here the value you typically put is a string a string is anything encapsulated in double quotation marks or single quotation marks and then this will be what is outputted so the first thing i'm going to do is just kind of welcome my users to the game i'm going to say welcome to my and then computer quiz so the specific quiz i'll go with this computer quiz please feel free to go with whatever type of quiz that you want all right now we've welcomed the user to our game let's just quickly test this out in vs code to run your code you press this little kind of run button right here you're going to see it should open up a terminal down here and then notice it says welcome to my computer quiz because the program just ran awesome so now what i want to do is ask the user if they want to play my game if they say no i don't want to play then we'll just immediately quit the game so to do that i'm going to create a variable i'm going to say playing is equal to and then i'm going to use this function called input now what input allows you to do is ask the user to start typing something in in the console so inside of input in here you put what's known as the prompt now the prompt is kind of what appears before the user can start typing so for example if you're asking the user for their name you may have the prompt be name and then colon and then maybe a space and then right here after this space the user could start typing whatever their name was so in my case i would put tim and then whatever they type after the prompt and then when they press enter that will be stored in playing so if i type tim and then i press enter now playing will be equal to tim because after the prompt that's what the user typed uh before they hit enter right hopefully that kind of makes sense and even if the user has a space they say like tim and then r all of this tim space r will be included in playing because this is all the stuff they typed after the prompt before they pressed enter so i'm going to ask them here do you want to play question mark and then i'm going to add a space here the reason i'm adding a space is because if i don't add a space then the user is going to start typing right on the question mark and that's going to be kind of all smushed together and it doesn't look nice and so we're going to add a space to make sure the user starts typing one space after the question mark all right so now if i run my program so i press the run button it says welcome to my computer quiz do you want to play notice the cursor is here and now i can start typing whatever i want when i press enter nothing's happening because we haven't done that yet we haven't configured that but that's kind of the basics that's how you get user input and just to show you here if i print out playing what this will do is give me whatever the value of this variable is so if i run this here it's welcome to my computer quiz do you want to play say yes and then it prints out yes right so pretty straightforward okay so now that we know if the user wants to play or not right because after this line is done we'll have something stored in playing we want to check if the user typed yes right and specifically if they didn't type yes we want to end the program so what i'm going to do is use something known as an if statement now an if statement allows us to conditionally check kind of or compare values together and see if something is true or false and so i'm just going to write out the kind of entire if statement then i'll run through it step by step i'm going to say if playing does not equal 2 so an exclamation point and then the equal sign and then i'm going to say yes then what i want to do is i want to quit the program and there's this function in python that you can use called quit this will just immediately terminate the program so what i'm doing is using if i'm writing what's known as my condition this is the thing that i want to check so i want to check if the variable playing which is what the user typed in is not equal to yes the reason i'm checking if it's not equal to yes is because if they typed anything other than the word yes i want to quit the program whereas if they typed yes i don't want to quit and so what happens here is we're going to compare whatever the user typed in with yes and since we're using not equal to if what the user typed in is not equal to yes this condition evaluates to a boolean which is known as true and then if this is true right so if whatever the condition is is true then whatever is indented after this colon here is going to be run so hopefully that makes sense i won't go too much more into the syntax i assume you guys can probably figure it out yourself but the idea is you put a colon and then all the stuff you want to happen when this condition is true you indent here and an indent is kind of four spaces i would recommend you use tabs rather than kind of going one two three four you'll get some issues with your indentation if you try to do spaces and tabs together so anyways let's just see this now uh let's run this program okay i'm going to say welcome to my computer quiz do you want to play i'm going to type yes and well the program is going to end regardless because we're not doing anything after this if statement but if i go here and i print okay exclamation point and then let's play smiley face um oops okay i need to fix my quotations here yeah okay that's all good let's run this now it says welcome to my computer quiz do you want to play i'll type yes and it says okay let's play right and then it gives a really bad smiley face that i need to fix and if we run this one more time and then this time we type no notice that it doesn't say okay let's play because this condition was true and so the program quit alright so that's about if statements hopefully that all makes sense we're going to be using a lot of if statements in this video so now we say okay let's play what we want to do is ask the user their first question and so what we're going to do is the exact same thing we did when we were asking the user if we want to play we're going to say some variable i'm just going to call it answer because that seems to make sense is equal to and then input and then i'm going to put whatever the question is i'm going to say you know ask something about a computer i'll say what does cpu stand for question mark and then what i'm expecting is that the user is going to type in the answer right and so what i'm going to do is just add a space here to make sure that again the user's not typing right beside the question mark they have a little bit of space and now what i want to do is check if the user's answer is equal to the correct answer so i'm going to say if answer is equal to and then i need to type out what the actual answer is now in this case this is central processing unit and i think i spelt processing correct obviously you're going to want to make sure your answers are actually spelled correctly because even if the user types in central processing unit but they like forgot the g or they spell something wrong or they have like a capital p they're going to have the question marked as incorrect because the answer has to match exactly with what the user typed it okay so now we have a colon and now we're saying okay well if the answer was equal to central processing unit what do we want to do i'm going to print that they got it correct so i'll say correct like that and notice i'm using single quotes and double quotes kind of interchangeably it doesn't matter which one you use so long as you're consistent with the starting and ending quote awesome so let's just run this now and see if this works so i'm going to press run it says welcome to my computer game do you want to play yes okay let's play what does cpu stand for central processing unit and then it gives me the the output saying correct now if i run this again do you want to play yes okay let's play what does cpu stand for i type in just cpu it doesn't give me anything because well this was not true sweet okay so now the thing is though if the user gets this wrong i want to tell them that they got it incorrect right we need some output saying them saying hey no that was not correct and so what we're going to use now is what's known as the else statement so whenever you have an if statement like this and you are checking if something is equal to something else and that's what the two equal sign is doing or you have some condition here if this condition here does not evaluate to true you can put this else statement here which means if this is not true whatever's in the else statement will run so if i go here and i say print incorrect exclamation point now if this is false so if the answer does not equal central processing unit then we will print out incorrect if we type anything doesn't matter just so long as this right here that we typed out is equal to false whatever's in the else here will end up run so let me just show you what i mean and then we can talk about this a bit more so let's run do you want to play yes okay let's play what does cpu stand for central processing unit and then it says correct and oops i i should have typed in the incorrect one but that's fine let's just type in no and then notice it gives me incorrect and if i ran this one more time type yes and i type some other random thing it still gives me incorrect so if i type anything other than central processing unit the else statement runs again the syntax for the else statement is you write else you do a colon and then any stuff you want to run you do indented after the else and you could do multiple things as well i could do another print statement and then both of these print statements would run uh if we got this question incorrect sweet okay so now we have our first question and now what we can do is kind of just copy this to do the rest of our questions right so i'll copy this uh one more time i'll do four questions for my quiz please feel free to do more and it's totally fine if we want to leave this answer variable the same i don't need to change this to be something else we could if we want to but since we are done using the answer for this question after this if statement it's fine if we use the same variable to store the answer for our next question so now what we're going to do is we're going to kind of remove all of our answers and questions and we're going to type in well new answers and new questions and i'll show you some more things that we can do here in kind of some fixes and all that as we go through but let's just get our questions done first all right so now my next question i'll say what does gpu stand for question mark and then this will be and i'll copy this graphics processing unit okay so let's go graphics processing unit like that i hope that's correct and then the next question we can ask um i'll just do a bunch of acronyms what does ram stand for question mark this will be random access memory like that okay and then lastly we can say what does psu stand for question mark and we'll just say power supply like that sweet so now we've got all of our different questions here and we're kind of printing out whether we got them correct or incorrect so let's just quickly test this out let's make sure this is all working so uh do you want to play yes what does cpu stand for central processing unit what does gpu stand for notice i didn't add the space here and since i didn't add the space you're going to see when i start typing here that's kind of smushed with the question mark so i'll say graphics processing unit what does ram stand for random access memory okay what does psu stand for let's just type psu and then notice it tells me incorrect so i'm going to start by fixing these spaces here you guys saw what happened when i didn't add the space for the input so now it should be all spaced out properly sweet so now we've got this all working hopefully you guys are all familiar with this if and kind of l syntax now it just needs to be in this structure whenever you have a colon typically in python the next line after that does need to be indented if i did this and like i didn't indent this line here you can see we're getting this red highlight and it's the reason we're getting that is because the indentation is all messed up this is supposed to be indented after we have the else statement in a small note you can only have the else statement after an if statement right so i can't just randomly come down here and put an else statement it needs to come after an if statement appears right so hopefully that's clear okay so now the one thing that i want to show you though is that we might be getting some kind of weird results from our program that we don't expect so i'm going to run my program and say do you want to play i'm just going to type yes with a capital y and notice when i do that it doesn't actually start my program right it immediately quits and the reason for that is whenever you have a capital that is different than lowercase right so if you have yes that is not equal to yes these are different strings and so if we want to check if the person's answer regardless of the case right if they have a capital y or capital e or a capital s or something or you know multiple capitals if we want y cap or capital y capital e lowercase s um to be valid and to allow the user to play we need to do something now there is this method in python called dot lower and what dot lower does is it takes whatever text we type in and it just makes it completely lower case so if i go here and i print you know tim is great like that and then i say dot lower and actually let me do this in another way that's not as confusing let me say text is equal to this and then i say text dot lower what this will do and make sure you have the parentheses by the way is it will convert all of this text to lowercase so i just run my program here and we have a look at our terminal notice that uh wait oh i have to type something let's just type that it says tim is great all in lower cases and so we can use this here to convert all of the answers that our user types in to lowercases so i can say playing.lower and then if the user typed in something with capitals we'll just all be lowercase and that way if they type yes with any different casing it will still evaluate to true because we're converting it to lower now in the same fashion there is something called upper if you say dot upper it will convert everything into upper cases and so then what you would do if you're using dot upper is you would make your answer in complete upper cases because you know you're always going to be converting anything lowercase to uppercase so hopefully that's clear but we're going to just throw lower on here to all of our answers so i'm going to say lower i'm going to say lower i'm going to say dot lower going to say dot lower and alternatively if you didn't want to do it this way you could actually just do it at the end of this input here you could say input dot lower and then you could just remove this because now your answer is already converted to dot lower so you don't need to convert it kind of in this condition right here okay hopefully that's all clear but yeah that's just taking your answer converting into lower case and then you're checking if the lowercase answer is equal to well the lowercase answer you actually have all right so let's rerun this i'm going to open up my terminal i'm going to run do you want to play i'm going to type yes with a capital y notice this works now and now i'm going to do some capital so central processing unit that works right gpu graphics processing unit and then you get the idea i won't do the rest of them okay so the last thing i want to do here is implement some notion of score right we want to be able to tell how many questions our users got correct so what i'm going to do is make a variable here at the top of my program i'm going to say score is equal to 0. this is going to allow us to keep track of how many correct answers they have now all we have to do here since we define score equal to zero is every time the user gets a question correct we just need to increment square so add one to score so inside of this if statement here because this is if the user got it correct right i'm gonna say score plus equals one what that says is okay take the value of score and add 1 to it that's equivalent to saying score is equal to score plus 1. so hopefully that's clear but that's a way that you can just add 1 to the variable and so if we add one to score every time we get something correct that means that at the end of our program we'll have some number right we'll have two three zero whatever however many questions we got right we can then use that to display how well the user did so i'm going to just say score plus equals one i'm going to do this inside of all of these if statements here okay and now we've added one to score so now what i can do at the very end of my program once we're done of our questions is i can say you got and then i need to use a plus sign this is a concatenation operator i'll discuss this in a second score plus and then questions correct exclamation point but i need to convert this to a string so let me go through what i just did here okay so i said you got i did a space notice i manually added the space here i then had a plus sign and then i said string score and then plus questions correct so the reason i'm doing this string here is because score is a number right score is an integer it's not a string and so if i tried to do something like you know tim plus one well this doesn't really make much sense right because i'm trying to add a number to a string and like how do you add one to text that's just an undefined operation that doesn't exist so what i need to do instead is convert this one to a string because now that i have two strings when i add them together what i get in python is tim1 right they kind of get combined and so that's what i can do i'm going to convert this score here into a string so then when i add it to the other strings that i have here it's valid this operation works and i'm not getting some issue popping up okay and then i'm adding the questions correct so i'm kind of putting this string score in the middle of the string right you got whatever the number is questions correct okay so let's run this and you want to play yes let's say let's cpu stand for central processing units okay graphics processing units okay psu ram and it says incorrect you got two questions correct right now it's giving me how many questions i got correct and so the very last thing we can do here is we can copy this line and we can give them kind of a percentage right like you got 50 or you got whatever percent and the way we do that is now we're just going to take our score we're going to divide it by the number of questions that we have is 4 and then we're going to multiply all this by 100 right and if we want to be extra clear here what we can do is put parentheses around this score over 4 to make sure this operation happens first before we multiply by 100 but you'll see if we did it the other way around we didn't add the parentheses it would still work fine because we would do the division before we did the multiplication but anyways we're going to say now instead of you got questions correct we're going to say you got and then we're just going to add a percent on here now notice i'm not doing a space and since i'm not doing a space it's going to be whatever this number is and then plus and then the percent sign so it'll come right after and then we can do a period so now we've calculated the the percent again just taking the score dividing it by our number of questions which is 4 and then multiplying it by 100. now note here if you change the number of questions you got to change this number right so let's go here and let's run this and let's go to our terminal yes we'll play i'll just type something random okay random access memory correct and then power supply and then it says you got 50 you got two questions correct and with that that is going to wrap up our first python mini project so i know i went really slowly through this and really explained everything in depth that's because this is the very first project i'm trying to make sure this video works for everyone regardless of your skill level and now i will go a little bit faster i won't re-explain a lot of the syntax that i've already explained now as we move into the new projects just anything new that i kind of come across that we use i will explain but this is our first project this is the quiz game there will be some code that has or there will be a link sorry that has this code in the description if you want to download this yourself but i would highly recommend you modify this you know change around maybe add like more than one point for questions that are worth you know more than one point that are more difficult or something like that right and you guys can do this as you please regardless that is the quiz game now we're gonna move on to do a number guesser i think this is the one i was saying i was gonna do second so this one is pretty straightforward but what we're gonna do is generate a random number and we're gonna track how many times it takes the user to guess this number and so what i'm going to do is zoom in a bit here so that we can see our code the first thing i'm going to do is import this module in python called red alright so first thing to mention here is that this right here is what's known as a module so when you import random you're importing the random module now this just lets you use all of the functionality that's kind of in here this is default this is installed by with python by default you don't need to install it or anything like that in quick note there is modules that are built by you know people like me or other developers out there and you can import them but you have to install them first this one is just built into python by default and so no installation is required anyways let me show you how to make a random number so there's a few different ways to do this but the two simplest ways are to do random dot and then rand range then you're gonna do an open and closing parentheses and let me get rid of the print statement so we can see this better for right now and here you're gonna put the start and then the stop of the range for your random number so you would do something like negative 1 if you wanted to start the range at negative 1 and stop at 10 excuse me and then what this would do is generate a random number that goes from negative 1 to 10 but does not include 10. so that's very important the number you put here is the absolute upper bound you cannot have the number 10 be generated if 10 is here if you wanted the range to generate a number between negative 1 and 10 you'd have to put 11. again i don't know why they do that there's just some things in python that aren't necessarily intuitive for beginners but with this is 11 it's not going to generate 11 it will generate up to 10. so make sure that's clear now there's another thing you can do here if you want the range to just start at zero you can say random.rand range and just put the stop if you just put one thing here what this will do is generate a random number between zero and this number minus one so up to ten and yeah there you go that's how that works okay so let's do something from negative 5 to 11 and let's say r is equal to random.randrange and let's just print r okay so let me run this and we'll look at our thing here okay run and notice we get a five now if i run this again we're getting a negative three i run a bunch of times we get a bunch of random numbers you see this is random and there you go that's working we got a nine we can just round a bunch of times we got a 10. notice we won't get 11 if we do this sweet so we're getting a random number so that's one way another way to do this is with a function called randint now rand int works the exact same way as rand range except now it will include 11. so the upper bound range now includes the number that you typed doesn't go up to but not included so now if i do negative 5 to 11 11 will possibly be generated so if i run this now uh we get 6 right and we if we keep running it we might see that we get 11. um okay i'm not this could take a while there you go so we do get 11. so that works okay sweet uh and same thing here if you decide to not include the start it will just generate up to and including this end number that you put right here so that's how you make a random number so what we want to do is generate a random number so actually let's let's leave this here let's say random underscore number and then we want to ask the user to guess this number and every single time they guess it we're going to tell them if they were above or below the number so that they can do it in like a reasonable number of guesses especially if we make this number like really large right so the first thing i'm going to actually ask the user is how large of a number they want to generate so we'll assume it starts at 0 and then we'll ask them to type in a number and we'll generate up to that number so i'm going to say let's say top of range is equal to and then input and we'll say type a number won't even tell them what it's for we'll just say type a number okay so now they're going to type a number so what we actually need to do here is we need to make sure that this thing they typed in here is a number because if the user types in like hello or tim or some random string something that's not a number then we can't use that right and we also need to make sure the number they type in is greater than zero so i'll show you how we do that i'm going to say if the top of range dot is digit okay and then i'm going to do an if statement inside of this if statement i'm going to say if the int and actually we're going to do this we're going to say top of range is equal to int top of range and let me pause here and explain what i'm doing so first of all you can make a variable using underscores when you want to have multiple words it makes a lot of sense to make the name with underscores so like top underscore of underscore range you can't have a variable that's like top range that won't work you need to have it connected you can't have like two separate words like that anyways this is equal to input the user is going to type something in now by default when the user types something in it's going to return it to us with double quotation marks so it's going to be a string so even if the user types in like 25 it's going to be returned to us like this and this here for python is not considered a number it's not considered an integer it's considered a string and so we need to convert this 25 so that it looks like this so that's actually a number and the way you do that is you use this function called int so you surround the string with int and then it will convert this string into its numeric representation so it will give us 25. however if you try to convert something that's not an actual integer into an int it's going to fail you're going to get an error and so we need to make sure that what the user typed in is a digit before we try to convert it into an it and the way you check that is with this is digit method so i say if top of range dot is digit what is digit will do is return to us true if this is a number otherwise it will return false and so if this right here is true we will convert top of range into an integer so top of range is equal to int top of range then we will check if top of range is greater than zero so we'll say if top of range is actually we're going to say less than zero less than or equal to zero specifically then what we will do is we'll print out please type a number larger than zero next time and then we'll just quit so we'll tell them what they did wrong and then we're just going to quit the program right there otherwise if we make it through all this we're good but one thing we need to do here is we need to put an else and we need to put quit and we need to put a print statement that says please type a number next time so let me run through this what we're doing is checking if it is a digit if it's not a digit so in the else statement we need to tell them hey you got to type in a digit it doesn't work if you don't type in a number right and then we quit now if that works if it is a digit we convert it into an int we make sure it's greater than zero if it isn't then we tell them hey you got to type in a number and then we quit but if we make it through all of this and we don't quit so we've now converted the number to an int we can now use that number to generate our random number so we can say randomnumber random.randint and then we can put top of range as our variable for generating that random number and we'll now generate up to whatever number they typed in sweet so now that we have that let's just print out the random number and let's just test this and make sure it's all working so i'm going to run this type number let's type five and we got one issue rand it missing one potential argument b okay so this is my fault i was saying when we used rand int we could just include the top of the range that's false we need to include the bottom of the range as well so i'm going to put 0 there and now this should work so let's run this and let's type a number let's type 5 and there you go we generated a random number which was 4. if we run this again type 5 we get 3 you can see this is indeed working sweet so now that we have our random number we want to keep asking the user to type in a guess for the number until they get it correct and so i'm going to use a while loop here and i'll explain how this works in a second i'm just going to say well true now what this means is okay we're going to do whatever is indented after this while loop here after the colon while this condition whatever's at the top of here is true so right now if we just decided to like print tim this would happen infinitely this loop would never stop unless we actually like manually close the program the reason is because there's no way for this to end right i say while true well true is always true and so we're just always going to print 10. however there is this keyword it's called break and what break will do is immediately stop the loop as soon as this line of code is executed so what we can do is use this break keyword to break out of the loop stop running the loop as soon as the user types in the correct number so we're going to say while true this is the condition for the while loop we could do a real condition we could say something like wow um like input and actually we'll say like user guess is not equal to random number we could do something like that that would work as well but i want to show you using the break keyword so instead we're going to do well true all right so what we're going to do is we're going to start by asking the user to guess what the number is right that's the very first thing we do every single time we ask them we say okay make a guess so i'm going to say user underscore guess is equal to and then input i'm going to say make a guess call now same thing here we need to check if the user's guess is actually an integer right so i'm going to copy exactly what we just did right here and i'm going to paste it down below and i'm going to say okay if the and now instead of saying top of range i'm going to say user underscore guess dot is is digit then user underscore guess is equal to int of user underscore guess and then i'll even take this and we don't need to check if the number is less than zero that's fine uh we can just do this so i'm going to say and i'm going to get rid of this this quit okay let me explain this but what i'm saying is okay the user guess is equal to input make a guess they're gonna type in some number this comes in as a string we need to make sure that what they've typed in is actually a number if it is a number we'll convert it to a number we're all good however if it is not a number then we're going to say please type a number next time and we're going to use this keyword called continue now what continue will do is automatically bring us back to the top of our loop so it's a little bit different than break rather than ending the loop it just brings us back to the very top so if i did something down here like printed after continue if we hit this continue keyword this won't run we won't print we'll just go back up to the top of the loop and ask the user to make another guess and so that's what we're going to do however we're going to come down here now we're going to say if the user guess is equal to the random number we're going to print and we're going to say you got it you got it correct otherwise we'll say else print you got it wrong so if they didn't get it right they got it wrong obviously right whereas if they didn't type a number we'll just say please type a number next time and then we'll continue rather than doing this and telling them they got it wrong after they didn't even type a number in okay hopefully that makes sense we can test this out though and make sure it's working so save the program and run now type a number let's type a number let's say 10 okay make a guess we're gonna guess three and wow okay we actually got it that's great but notice that it's still asking us to keep making guesses and to quit this you can type control c on your keyboard if you get into an infinite loop the reason it's telling us to do that is because we didn't break out of the while loop right we said you got it but we didn't stop and to stop we need to use the break keyword so now you'll see if we do guess it we'll immediately stop looping whereas if we don't get it we will continue looping because the break keywords not here so let's run this now type a number let's just go up to five make a guess one you got it wrong okay we'll guess two you got it wrong three you got it wrong four you got it wrong five you got it sweet and then we exit the loop there you go okay so that's working fine now what i'm going to do is i'm going to keep track of how many times the user actually makes a guess so to do this i'm going to make a variable similar to what we had with score before i'm going to say guesses is equal to 0. and now every single time we start this while loop again i'm going to increment add 1 to guesses so i'm going to say guesses plus equals 1. so at the very top of the loop we'll increment guesses so the first time this runs guesses will be equal to one we'll go through all of this then we'll come back up to the top guesses will now be equal to two the user will now make another guess and now when we exit this while loop once this is done we're gonna print out how many guesses they got i'm going to show you a different way to do this this time i'm going to say you got it in and then we'll say comma guesses and then the string guesses so another way to print multiple things on the same line is to use commas so i'm going to say you got it in and then i'm going to say whatever the number of guesses is notice i don't need a space here whenever you have commas separating your different things that you're printing out it will automatically add spaces in between them and then notice here i didn't even convert this to a string and that's because the print statement is smart enough to say okay hey this is an int i'm going to convert this into a string for you and kind of automatically add it to this string so what we were doing manually before the print statement can just do for us and so this time we'll let the print statement do it uh this line here is completely equivalent to just having the uh the plus the space the string uh the other plus and the space right it's the exact same thing okay so let's go back to what we had and there you go we are all good now let's just run this and i am going to show you how we can tell them if they got it greater than or less than because i did tell you that we were going to do that okay so let's run this and let's say type a number let's go five make a guess one you got it wrong two you got it and then it says you got it in two guesses sweet all is working so now the last thing we want to do is tell the user if they were greater than or less than the number so that they can kind of narrow down their guesses and so rather than just telling them you got it wrong now inside of the else statement we're going to check if the user was above or if they were below there's a ton of different ways to do this i'm just going to do it this way i'm going to say if the user guess is greater than the random number then what i want to do is say print you were above the number exclamation point otherwise i'm going to say print and i'll say you were below the number all right so let's run through this we're just checking if the user guess is greater than whatever the random number is if it is we're going to tell them they were above the number otherwise if it wasn't greater than it must be less than and we're going to say you were below the number now some of you may have heard that statement if it wasn't greater than it must be less than you said no it could be equal to that's correct but the thing is up here we checked if it was equal to and so if it was equal to we would have gone into here we would not have hit this else statement so if we're in this else statement we know we must not be equal to the random number and so instead what we can do is just run this if statement that's inside of the else i'm going to show you a more elegant way to do this in one second but let's just run the program and see if this works okay let's run this type a number let's type 20 make a guess let's guess 10. you were above the number okay so now i don't have to guess below 10. so i'm going to guess 5 you are above the number okay it's less than 5. uh 4 3 2 1 you got it in nine guesses okay so anyways that did work but it was telling us whether we were above or below uh let's see if i can guess something that's actually i guess i couldn't have guessed anything below one so let's run this again let's type 10 let's guess one and then this time it is telling us we're below the number okay great so that is working okay so the one thing i want to show you here is how we can clean this up a little bit using something new that we haven't seen yet which is known as an l if statement so an l if statement is something that kind of happens after the initial if statement's checked so i'm just going to type this out and then i'll show you how this works so i'm going to say lf okay so now what i've done is i've made this code a little bit cleaner and the way i've done this is i've removed that kind of nested if statement that we had and i've now implemented an alif instead so what the life does is if the initial condition is not true it will then check another condition so it says okay if this is true we're going to do this we're going to skip all of this if this is not true we're going to check this if this is true we're going to do this and we're going to skip whatever else is after it however if this is not true then we're going to do whatever is in the else so we're going to check this if it's true we do this otherwise we check this if it's true we do this and otherwise if both of these things are false we do what's in the else and so that's just another way to do this rather than having what we have before which was the else and then all of this kind of indented in the else right this just cleans it up and small note you can have as many lf statements as you want so i could add another one like this if i wanted to do that but in our case it doesn't really make any sense to do that okay so that's actually it i'm not going to run the program just believe me this this does work and yeah that is going to be our number guessing game so i'll zoom out a bit so you guys can kind of see all of the code but that is how that works i guess there's not really much more to say about that and now we'll move on to the next one all right so now we're moving on to rock paper scissors now obviously you guys know how this game works what i'm going to do here is make it so that the computer keeps track of how many times the user gets it correct versus how many times the computer gets correct when i say corrects or i mean whoever wins rock paper scissors so the first thing we're going to do here is import random now small note here when you are importing modules it's typically best practice to do at the very top of the program i see some beginners import the module like right before they use it that's fine you can do that but it's just a better practice to have it right at the top so it's really easy to see what stuff you've imported and obviously remove it and then you can use it throughout the entire file whereas if i tried to use like random up here it doesn't work until after i've imported right so hopefully that's clear but anyways that's what we're going to do so what we need is we need two variables one for the user score and one for the computer score so i'm going to say user underscore wins is equal to zero and i'll say computer underscore wins is equal to zero as well all right now i'm going to have a while loop i'm going to say wow true and the first thing i'm going to do here is i'm just going to ask the user uh actually yeah i'll ask the user to input rock paper or scissors and then i will also let them type in q like the letter q and if they do q then we'll just immediately quit the program so let's handle that first let's say user underscore input is equal to input and i'll say type and then we can say rock slash paper slash scissors is scissors with uh with with two s's i think that's how you spell it okay rock paper scissors and well paper should be spelt correctly okay rock paper scissors or q to quit okay all right so now that we have that we will go down and we will check let me just zoom out so we can see this here what the user actually typed it so the first thing i want to do is i want to see if they typed in queue so i will actually convert this to dot lower in kind of the way that i showed you we could do it previously we'll take whatever the user types in and we'll just convert it to dot lower and actually instead of a period let me do a colon and let me add a space so it's not all smushed there okay so type rock paper scissors or queue to quit and now i'm going to say if the user input and this is converted to dot lower right is equal to q then what i'm going to do is i'm going to quit otherwise i'm going to check if they typed in rock paper scissors or if they type something else and now i'm going to show you how we can check if the user input is equal to more than one thing because previously i've been showing you how we can check if it's equal to just a string right but now i'll give you a little bit of i don't want to call it a hack but a little bit of a trick so that you can see if the user typed in rock paper or scissors all in one line rather than writing three if statements so i'm going to say if the user input is in and i'm going to use a list you guys probably haven't seen this before if you're like a complete beginner in python and let me just kind of fix this a little bit so p s i'm going to type it myself and i'll explain what's going on here okay so let's make this in quotes okay and then this needs to be in quotes two okay i'm really messing this up quite badly but you get the idea okay so what i've done is i've just created a list now a list is anything encapsulated in these square brackets um that is like separated by commas so i have this rock paper and scissors string in this list and so what i'm saying is if user input is in this list i'm checking if whatever the user typed in exists in either of these three strings so essentially we're just checking if the user typed in rock paper or scissors now you can put any strings in here that you want but we're just checking obviously if this is in here and that's how we can check multiple things on one line there's other ways to do this too but this is kind of one nice trick that we can use so i'm going to say if user input and i'm actually going to say is not in now this is another keyword again that you may not have seen before what this is going to do is simply reverse the kind of thing that we were checking before so we were checking if the user input was in rock paper scissors so now when i say not in i mean obviously as you would read in english this is checking if the user input is not in here if it's not in here that means they didn't type in something valid and well what we need to do is have them type in something valid so what i'm going to do is say type continue here and what this will do is just re-ask them to type in rock paper scissors or q so we could give some error message saying that's not a valid option or something like that but what i'm going to do is just have it so it keeps asking them to type something in until eventually they give us something about so when i hit continue that means anything after here is not going to happen right if this continue is hit we're just going to go back up to the top of the while sweet and actually rather than saying quit here what i'm going to do is say break so this will break out of the while loop and then that will actually automatically end the program for us because we'll be at the end we won't need to what he called actually manually quit so the break breaks out of the wall loop which means we'll go to wherever the end loop is done or whatever the while loop is done and then we can actually just print something saying you know good no not not that uh goodbye exclamation point sweet okay so now at this point in time the user has given us validimp they've typed in rock paper or scissors so what we need to do is generate a random number that represents rock paper scissors for the computer so i'm going to say rand i actually say yeah random underscore number is equal to and then this is going to be random.rand int and we'll generate a number between 0 and 2. so if they type in 0 that can be rock if they type in 1 that can be scissors or paper they type in 2 that can be scissors so let's write that down in a comment uh to do a comment in python you hit the pound sign or use the pound sign like the number sign and then type anything after it anything on a line that comes after a pound sign will not be read by your python interpreter and it's just there so that as a programmer it's easier to read and gives you like you know documentation or comments about what's going on so i'm going to say rock is one or rock is zero i'm going to say paper is one i'm going to say scissors is and then 2. sweet so the way that i'm going to do this now is i could do an if statement or i could write three if statements say you know if random number is equal to zero then the computer picked rock if random number is equal to one if random number is equal to two i actually don't need to do that what i can do is uh let's see here okay so what i'm going to do now here to actually assign some variable equal to rock paper scissors to represent the computer's kind of choice is i'm going to use a list now again this might be a little bit confusing but i think you guys will get the idea if i do this i'm going to take this list right here rock paper scissors i'm actually going to store it in a variable i'm going to say options is equal to rock paper scissors and i'm going to change this here to be options so this is the doing the exact same thing as before except we're just storing this list of values now in this options variable now if you haven't seen lists before these are actually very interesting they're known as a data structure and they are collections of elements right we have multiple elements multiple strings stored in this list and while the way we access these different elements in this list let's say i just wanted scissors or i just wanted paper i just wanted rock is we use what's known in known as an index so if you write the name of your list which in this case is options that's the variable storing the list you can put these square brackets and then you can put a numeric index that represents these different elements so in this case the different indices of this list are 0 1 and 2. and if we add another element right let's just go you know test then this would be index 3. so the way this works is you start counting at 0 and then you just go up by 1 every single time so the very first element is indexed by zero second one third two fourth three and so that's all that is so when i say options at zero that's going to give me rock when i say options at one that gives me paper when i say options at two that gives me scissors and so i have some random number that's between 0 and 2 right and 0 represents rock one represents paper and 2 represents scissors let me get rid of test so what i can do is say let's actually change this here say computer underscore guess or computer underscore pick i guess is equal to and then this is going to be options at whatever the random number is because this will be between 0 and 2 and so we'll use that as the index to actually access the string the thing that the computer chose right rock paper or scissors so now what we can do is we can print out what the computer chose we can say computer picked and then we can say comma and we could say computer underscore pick and then we will decide who actually won right so then we'll say who won and let me just do comma uh actually let's do plus and let's just add a period the reason i'm not doing a comma and then the period is because if i do a comma it will automatically add a space between this period and the computer pick which i don't want and so instead we're just going to add the the dot like that okay so we're going to say the computer picked that and then we need to decide who won right and so to decide who won we need to all compare uh the user's pick to the computer's pick so we're going to say if the user input is equal to and i guess we'll say rock we'll then do kind of the winning condition for the user so if the user picked rock then what do they win against well they win against scissors and so now we need to check if the user picked rock and the computer picked scissors so what i'm going to do is use this keyword called and and say computer underscore pick is equal to and then scissors so if that's the case we'll print out uh u one exclamation point and then we'll say user underscore wins plus equals one so that they get one more win so what this and does is it checks if the condition on the left side and the condition on the right side are true so what happens in this if statement or what's in this if statement will only happen if both of these things are true pretty straightforward that's how the and works alternatively there's an or and an orb will kind of do the opposite it will check if either the left or the right is true or both of them are true if any of those three things happen this is true this is true or both of them are true then what's in the if statement will run whereas with the and it has to be both of these being equal to true for what the what in the if statement is going to run or if what's in the if name is going to run sorry butchering my english okay so that's one of the win conditions for our user and then if that's the case we can just go ahead and say continue and we can go back up to the top of the while loop and keep going but we need to check the other wind conditions right and there's two more wind conditions let's go here and let's fix the indentation okay and we'll say if the user input now is equal to paper and the computer picked uh what is it rock then that means that we won right and so we would add one to the win and then the last one is we picked scissors and the computer picked paper sweet so the reason i'm doing this continue here is because after we've determined that we've won there's really no point in checking these other if statements so we could instead do these in l-ifs so i could say if l if elif and then actually i could remove all these continues and in fact let's do this and then we will go down here and we will say else and then what we would do here is say print u lost and then we will say computer wins okay plus equals one so the reason this works now is because we check if we won with the rock versus scissors we check we won with paper versus rock we check if we want to wear scissors versus paper and if none of those are true that means we didn't want win right there's no point in checking if the computer beat us in all the other scenarios we just need to check if our three win conditions were true if they are not true then we know that we lost and so we would print we lost and then we add our computer wins and this is an example of using multiple lf statements we'll check this one if it's false we'll check this one if it's false we'll check this one if it's false we run this if any of these are true we immediately stop and we do whatever is inside of here which is increment the user wins and print u1 great so the last thing i'm going to say before i do goodbye is i'm just going to print out the number of user wins versus computer wins so i'm going to say the user or i'll say in this case u u1 comma user underscore wins times and then we'll print the computer one and then computer underscore wins times period great so hopefully that is all good let's quickly run through what we've done because we wrote a lot of code here relatively quickly as you can tell i'm kind of picking up the pace a little bit as we go through this so we have our user wins our computer wins we're importing random which we've already discussed we have our list first time we've seen this called options has our three values in it we have while true we have user input equals input and then we say type rock paper scissors or q to quit we make whatever they type in lowercase we check if the user inputted queue if they do we break we're done we stop the program if they didn't then we check if the user input is not in the options if it's not in the options that means they didn't type a valid rock paper or scissors and so we press or we go continue which means we're going to start the loop again we're going to ask them to type something in then what we do if they typed in something valid is we generate a random number between 0 and two uh this is the like what it stands for rock is zero paper is one so this is two we make our computer pick so computer pick is equal to options at whatever the index is of the random number we then say okay the computer picked this we then check if the user won based on what the computer picked right and uh yeah that's all good and then we increment computer wins or user wins and we will keep going until eventually the user hits q once we're done we'll break out of the while loop and we'll print these three things let's run it let's see if it works okay type rock paper scissors or q to quit let's just test q okay you won zero times the computer one zero times goodbye nice works okay let's actually type something let's go rock it says computer pick scissors you won nice okay type rock paper scissors or cue to quit let's go paper uh computer pick scissors you lost okay that sucks let's go uh scissors computer picked paper you want nice let's go rock okay and let's go q and then it says the computer one one time you won three times good bye all right so that is rock paper scissors keeping track of how many times both the user and the computer wins now we're gonna move on to the next project which is the choose your own adventure game alright so this next project is one of my all-time favorite beginner projects and that is because it's so customizable there's so much you can do with this i'm really only gonna talk about this for about five or ten minutes because this is up to you guys to make this kind of as big as you want so this is a choose your own adventure game and i guess the best thing to relate this to if you haven't heard of this before is kind of these children books and actually they have books that aren't children versions too but they're kind of like pick your own story books where you'll go to a page right and it will say like if you want to go left turn to page like 42 if you want to like swim across the river go to page 97 whatever and then like it kind of brings you on all these different paths throughout the book based on decisions that you make so same thing here we're going to ask the user to make a bunch of different choices we'll start with something simple like do you want to play the game right and then if they say yes we'll say okay you know you come to some bridge do you want to cross it or do you want to swim underneath it and then based on what they say if they cross the bridge maybe we'll say okay you come to a dead end do you want to turn around or do you want to search the jungle whatever like you can be as creative as you want i'm just coming up with stuff at the top of my head but then if they had instead decided to swim under the bridge maybe we say like oh you know there was a crocodile and your your journey ended whatever so you guys can make it as complex as you want but i'll show you like the basic structure and kind of the nesting of statements and how it works so the first thing we're going to do is i'm actually going to ask the user for their name this is one this is something i don't usually do but i think this will be cool so i'm going to say name is equal to input i'm going to say type your name cool and then i'm going to say print and i'm going to welcome the user i'm going to say welcome comma name comma to this add venture exclamation point and then we'll actually get right into it and before i even ask them if they want to play what i will do is i will just say like ask them for a choice i'll say you come to a dirt road or something do you want to go left or right whatever so the first thing i'm going to say here is answer and lower cases is equal to input and i'll say you are on a dirt road it has come to an end and you can go left or right uh and then i will say which way would you like to go question mark okay so let's just go here and notice that it's doing this auto formatting you guys don't have to worry about this just my ide auto formats my code when i save it and so you can see it's kind of going on a new line uh don't worry about it you can just do yours in the same thing but anyways that's like my prompt you're on a dirt road it has come to an end and you can go left or right which way would you like to go and i'm expecting them to type in left or right so the first thing i'm going to do is i'm going to check if they typed left or right i'm going to say if answer is equal to left and actually before i do that i'm going to convert this input here two dot lower and i realized i kind of messed something up hopefully that fixed it okay so we're gonna say dot lower and let me add the space here between the question mark okay so answer equals equals left so if the answer equals left then we'll do something else otherwise uh we will actually we'll say l if answer is equal to right and then else we'll say print not a valid option you lose so i'm just going to make it so if they ever give us something that's not valid they just immediately lose the game okay so if the answer is equal to left then we need to pick what's going to happen when they go to the left if the answer is equal to right we need to decide what's going to happen if they go to the right so i'll start by kind of doing the left path and let me just put a print statement here so i don't get an error you'll notice that if you don't have something indented after it's expecting something to be indented it will like yell at you and be angry so you can just put an empty statement like print and that will kind of fix it okay so now what i'm going to do is i'm going to ask them another question i'm going to say answer and you can name these variables whatever you want maybe it would make more sense to name them like q2 or something whatever i'll say answer is equal to input and i'll say um okay like you come to a river and if you can walk around it or swim across and i'll say type walk to walk around and swim to swim across and make sure that when you guys do this you're kind of like uh you know telling the user what they need to type in because sometimes it's not intuitive on what they should be typing and so i always try to give them the option like tell them explicitly like what i'm expecting them to type so they don't give me an invalid option like not intentionally you know so now i'm going to check inside of here if the answer is equal to swim l if the answer is equal to walk and then else i will print not a valid option you lose right so we'll do that okay so now i need two things to happen here so print and print okay so i'm going to pause for a second hopefully you're getting the idea of what's going on here right if they go left i ask them a different question and then i check inside of this if statement what they answer for that next question and then i do something else whereas if they go right i'm going to ask them a different question and so it's cool because there's like all of these different paths that people can explore and like if they only go left at the beginning they're only exploring like half of the potential paths and then as soon as they go right there's all new different options now you could have the same options for different things they type right if they decide to go left or right you could in theory ask them the same question if they go left or if they go right but i like to do it differently i think that's more fun so let's just kind of continue on this path here let's say if the answer is equal to swim then i'll print um you swam across and were eaten by an alligator is that how you spell alligator maybe you guys can tell my spelling is not the best okay eaten eaten there you go buy an alligator nice now if they decide to walk across i'll say you walked for many miles ran out of water and you lost the game okay so both of these options here were inbound so if they decide to go left it doesn't matter what they chose they were just going to lose and so instead they have to go right and so we've kind of ended the path now on the left but if we wanted to maybe swim was like the correct answer quote unquote we could add another path here and we don't have to have them lose on one of these options i could add another path for walk too right like hopefully getting the idea you can just chain these kind of if statements in as far as you want so now let's do something for going right and actually before we do that let's let's test this out and let's make sure you guys understand what's going on here okay so type your name i'm gonna say tim says welcome tim to this adventure you're on a dirt road it has come to an end and you can go left or right which way would you like to go let's go left when i go left it says you come to a river you can walk around it or swim across type walk to walk around and swim to swim across you're gonna type swim it says you swam across and we're eaten by an alligator and then there you go boom you lose right okay so now let's go and do something for right so for write i'm gonna say answer is equal to input i'm going to say you come to a bridge it looks wobbly do you want to cross it or head back question mark and then the options i'll put here i'm just going to put them in brackets i'll say cross slash back to kind of really indicate hey that's the two things that you're supposed to be typing cross or back and then here i'm going to just copy this if else structure just so i can fill it in i'm going to say if the answer is equal to back if the answer is equal to cross and then i'll continue so now i'm going to say if answer equals back you go back to the um i don't know like main road whatever we can make this whatever you want now you can decide to drive forward i don't know drive to the left or go to the right or you know what we can make going back just just be the wrong option we'll just say you go back and lose i i don't think you guys need me to uh to give too much detail here because i'm having trouble coming up with this okay so we're going to say that crossing is the correct option so if they decide to cross then what we're going to do here is now we're going to ask them another question we're going to say answer is equal to input we're going to say you cross the bridge and meet a stranger do you talk to them and we'll say yes comma no question okay and then same thing here we can say if answer is equal to yes uh l if answer is equal oops is equal to no and then else we'll say not a valid option you lose so let me copy that okay not a valid option you lose and then here again we can decide what we want so we'll say print maybe yes is like the correct answer and it ends the game we'd say um you talk to the stranger and they give you gold whatever you win exclamation point uh otherwise no we could say print um you ignore the stranger if i could spell stranger correctly and they are offended and you lose okay so again i i'm just like randomly coming up with this stuff i'm sure you guys are laughing at the options that i'm i'm going with here but i'm just trying to show you the structure of this program and kind of how like the nesting works that you can make this as advanced as you want so now we've kind of implemented something for the right so it asks you a question uh obviously you lose if you go back uh if you go to decide to cross then you're asked another question you cross the bridge meet a stranger do you talk to them if the answer is yes you lose are you in if the answer's no you lose if you answer something that's not one of those then what do you call it you lose and then of course if you wanted to keep going from here rather than telling them they lose or they win you would just ask them another question and you would keep doing this and keep nesting this if and else and you can make multiple options too you don't have to just do two options you can make it so they could give you three options you can make it so that you store one of their options in a variable like name right and then you use that variable later on like maybe they picked up a i don't know like a weapon or something really early and if the weapon they chose really early is not correct when they go down a certain path then maybe they lose whereas if it was correct then maybe they can continue on or something like that and so lastly i'm going to say print thank you for trying comma and then this is going to be their name okay so let's run this and i'll just go through a few of the paths but that's pretty much gonna wrap up this game there's not really much more to show you uh this is just a cool one and i always find this fun and obviously you guys can come up with better stories than i can so type your name and say tim welcome tim this adventure you're on a dirt road just come to an end you can go left or right i'm going to go right okay you come to a bridge it looks wobbly do you want to cross their head back i'm going to cross you cross the bridge and meet a stranger do you talk to them no says you ignore the stranger and they're offended and you lose thank you for trying tim right there you go then we could try this again type your name tim uh you're on dirt road let's go left right you come to river let's walk and it says you walked for many miles right under water and you've lost the game i'm not going to go through all the options i think you guys get the idea now that is the choose your own adventure game again this code will be linked in the description if you want to download it but this is kind of the template use the if elif and l structure and you know make this as advanced and complex as you want now let's move on to the last project which is the password manager all right so now we're moving on to the last project which is a password manager now the point of this program is to kind of organize and store your passwords so we're going to store all of the passwords along with the username of the account they're associated with in a text file but we are not going to store the passwords in plain text we're going to encrypt the passwords now even though we're encrypting these passwords which means they're going to need a kind of password to be able to decrypt them so there's like a master password for all of the passwords this is not a secure way of storing your passwords do not rely on this for anything other than like a fun python project and i personally would not recommend storing at least any of your very sensitive passwords uh in this method it's just like a cool thing and i wanted to show you how to do it and it's kind of a more advanced project out of the five that we've done so the first thing i'm going to do here is i'm going to going to write the code that's allowing us to actually ask the user whether they want to list out their passwords or whether they want to add a new one and we're going to store all of these in a text file so i'll do this project first without encrypting any of the passwords and then we'll go in and encrypt them so the first thing that we need to do is ask the user for like a master password so we're going to say pwd is equal to input and we'll say what is the master password question mark now we're not actually going to validate this password as equal to something we're going to use this password to encrypt our our passwords so if they type in the wrong password here it's still going to work like the program will operate but it won't show them the correct password and you'll see how that means when we kind of get later into the into the video uh regardless what i'm going to do now is ask them what mode they want to go in so whether they want to add a new password or whether they want to view their passwords so i'm going to say mode is equal to input and we'll say would you like to add a new password or view existing ones question mark and then the two inputs i'll allow will be i guess view and add okay so that's what i'm expecting them to type in now what i'm going to say is if mode is equal to view we will do something otherwise l if mode is is equal to what i say add we will do something else otherwise else will say print in valid mode and i'm going to take all of this i'm going to put this inside of a while loop so i'm going to say while true because we'll allow them to like add a password then view them and like so on and so forth and so in this else here i'll just say invalid mode and then i'll say continue and what this will do will bring them to the top of the wall and i'm also going to add something here that says press q to quit and then i'm going to convert all of this to dot lower and then i'm going to check at the very top here i'm going to say if mode equals equals q then break the while loop okay sweet and then we could make this an lif but we don't have to make it in life since we have a break here uh if this is true it just won't even bother checking these it's up to you but i'm not going to add the the elif okay so now that we have this what i'm going to do is show you something called a function so i'm going to create two functions here i'm going to say define view and i'm going to say pass and then i'm going to say define and this is going to be at okay so what a function is is an executable reusable block of code so the way you call a function is you write the name of it which is this okay and then two parentheses so the basic syntax is you write def to define a new function space name of the function open and closing parenthesis and then any parameters that you want inside of here i'm not going to really talk about what that is because i'm not trying to teach functions here i'm just trying to show you how we can kind of split our code into different sections and anyways the way you call a function is by its name and then the open and closing parentheses so if i put say like print tim inside of here then i could call this function a bunch of times and every single time i call this function it would do whatever is inside of here which would print tim so if i had you know print him twice every single time i call the function it's going to print out tim two times whatever is indented inside of here just happens when you call the function pretty straightforward but the point of me creating these two functions is that rather than writing all of the code related to the view mode and the add mode in the while loop i'm going to put them in this function so it's a little bit more organized and easier for me to kind of separate my program so i'm just going to have pass inside of here right now now pass is a keyword that literally does nothing it is just there to make it so you don't get any like indentation errors because you need to have something indented after the colon and so for now we're just going to write pass and you can see we've done that here too so now instead of just using pass i'm going to call the view function i'm going to call the add function so now depending on what mode you select it's going to call these two functions which will then perform the operations for that mode it's just a cleaner way to write the code it makes a little bit more organized okay so let's start with the the add function what i want to do is create a new file if the file storing our password just doesn't already exist and add the password into it so the first thing i need to do is get the user account or whatever the username and then the password and then i want to add it into the file so i'm going to say i guess name is equal to input and we'll say account name like that and then we'll say pwd which stands for password is equal to and actually sorry this should just say password because we already have pwd up here and in fact let's call this master underscore pwd and then we can call this pwd so pwd is equal to input and we'll say password colon like that so now they're inputting their account name and their password now what i want to do is open a file or create a file if it doesn't already exist and add this password in so i'll explain what this is doing in a second but i'm going to say with open the name of my file which in this case is going to be passwords.txt we're going to just use a text file for now then we're going to say comma and then we need to define the mode to open this file with now the mode we're going to use is a which stands for append we're going to say as f and now i'll explain what i just did so when you use this with thing right here this allows you to then do some things indented after it and the point of this width when you're opening a file specifically is that as soon as you are done doing all of the operations with the file since we use this with it will automatically close the file for us so you can open a file like this you can say file equals open and there you go now you have file but the thing is if you do this you need to make sure you manually close the file after you open it because if you don't do that your python process will still be kind of like holding on to and using this file and it's going to cause problems if you try to open this file somewhere else and so it's just better and safer to kind of use this with keyword because now you don't have to manually close it it will automatically close the file for you so anyways i'm going to say with openpasswords.txt in a mode now so this is the name of the file the next thing is the mode to open the file in now there's a bunch of different modes the main ones are w r and a now w means write and what this will do is create a new file or override this file if it already exists very important you understand this if passwords.txt already exists and you open in w mode it will clear that file and make an entirely new one so only use w mode if you want to always override a file that potentially already exists okay then there's r mode this is just simply read mode you can't write anything into the file if you open it in read mode all you can do is just read the file that's r mode and obviously this is not going to destroy a file if it already exists in fact there'll be a problem if you try to open the file and it doesn't exist in armor then you have a mode this is append mode this is probably the most flexible mode what this allows you to do is add something to the end of an existing file and create a new file if that file does not exist so if passwords.txt does not exist a new file will be created called passwords.txt and then we can write to the file if passwords.txt already exists we are able to write to the file and read the file from the very end uh so sorry we can write to the file from the very end and we can just read the entire file when we open it in a mode and so for adding something we're gonna open in a mode because if it exists we append to the end if it doesn't we create it awesome so now what we're gonna do is say f dot write that's the name of our file because we said as f and then we are going to write in the name plus the pipe plus pwd so what this is going to do is take the name it's going to separate the password with the pipe operator and then it's going to put the password and then we will have user and password inside of the file so let's actually try this out let me run this you can see it says what is the master password i just type whatever for now just type hello and would you like to add a new password or view existing ones view comma add press q to quit okay i'm going to type add and it says account name let's just go tim password let's go is underscore great okay so now i'm just going to quit notice this passwords.txt file has been created if i open it i now have tim and is great and if i were to do this again it would add the next line in so actually let me rerun this let me say add account name joe password is billy and then notice it gets added in here the thing is though it got added on the same line and i don't want this to be added in the same line and so what i'm going to do is i'm going to quit here and i'm going to make it so that i go to the next line after i add each password and the way i do that is i add what's known as a line break i think this is actually called a carriage return maybe a line break at the end of the line so every single time i add a line i tack on this little invisible character backslash n this won't actually appear on the line you won't see this on the line but we will add that on and what that tells the text editor to do is to go to the next line so now the next time we write something in it will be on the next line so if i look at this and we run this now add tim a and 5432 and now if you look here it goes to the next line okay so hopefully that makes sense but uh there you go i'm just going to delete everything in that file i'm going to quit this and now we are able to add passwords and usernames in to the file what i'm going to do now is make it so that we can view all of the passwords so to view all of the passwords i need to again open this file so i'm going to copy this i'm going to say with open and then this is going to be passwords.txt a as f i'm going to change this from a to br because i don't want to potentially create a new file anything like that i just want to read the existing file and then what i'm going to do is i'm going to loop through the lines of the file and just print them out so i'm going to say 4 line in f i'm going to say dot read and actually read lines is what i want to do now what read lines is going to do is just exactly what it says it's going to take the file sorry this should be f it's going to read all the lines for it and then what i can do is use a for loop to get all of the lines of the file so let's just look at this i'm going to say print line and let's just see what happens now if we go to view mode and then i'll kind of walk through this more so let's run master password doesn't matter let's say view and then oh there's nothing in the file so it didn't show us anything okay so let's go add account name let's go tim password test and now let's say view and then notice it shows us tim and test but it's printing a new line after it shows us this account name and this password and so i'm going to show you how we can kind of fix this now so the thing is remember how i told you were adding this kind of invisible backslash at the carriage return now when we read the file we actually read in that carriage return and so we need to strip this carriage return from our line and the way we do that is we use this thing called r strip now what our strip will do is it will strip off the carriage return from our line so that's all you have to know just r strip that's what it will do there's also something called strip and strip will strip off the carriage return as well but our strip is like more specifically exactly for this so what i'm going to do now is run the file or run the program master password doesn't matter i'm going to say view and now notice that it's not printing that new line after we print this line because we stripped off the character chain great so now the thing is though i'm printing tim pipe test i kind of want to figure out or separate the username from the password so how are we going to do that well i'm going to say that my data is equal to line.rstrip and then i'm actually going to split this data so i'm going to say my user and pass is equal to and sorry this should be pass w is equal to data dot split at and this is going to be a pipe operator now this might seem really confusing because again this is a beginner project i know a lot of you guys haven't seen the syntax before what dot split will do is it will take a string it will look for this character right here and it will split the string into a bunch of different items uh every single time one of these characters is found so if you had like hello pipe tim type yes pipe two whatever what would happen is this uh data.split would actually return to you the following it will return to a list that says hello tim yes and two so remove all of these pipe characters and it would give you all of the unique strings that are between these pipe characters in a list format and so to access these different elements you would use index 0 1 2 3 so on and so forth now the thing is we know we're only ever going to have one pipe operator right unless the the password or the username contains a pipe operator but we're just going to assume that it doesn't contain a pipe operator just to make our life a little bit easier if it did contain a pipe operator there'd be some more steps we have to do i'm not going to do them for right now anyways we know we're only going to have one pipe operator which means the list that's going to be returned to us will only have two elements in it in fact it will always have two elements in it so what that means is that we can actually just grab both of those two elements by saying user comma pass w so when you do something like user comma pass w this assigns the first element in the list to user and the second element in the list to pass w and since we know our list always has two elements that means we can do this right whereas if we had three elements we would then have to add another variable right and then x would be assigned to x hopefully that makes sense but that's why we can do what we're doing and that's kind of what the split operator does so now what i'm going to do is i'm going to print out user colon comma user and then password cola comma password or passw right so now it'll look a little bit nicer so let's go ahead and try this i'm going to quit this program i'm going to rerun it it's going to say what is the master password say test i'm going to view so now when i view it says user tim password test and i think i should do like a separator between the user and the password and so what i'm going to do is just add a comma like that okay so now if we run this let's quit let's rerun master password test would you like to add a new password i'm going to say view it says user tim comma password test and there you go and we could actually even split this up with a pipe operator i think that will look cleaner i'm not going to run it now but we'll do with the pipe sweet so now we're able to add passwords and we're able to view them but the problem is i can just look at this file and i can see what the password is and well that's not very good we don't really want to do that so we need to encrypt these passwords all right so how do we encrypt a password well this is where it gets a little bit more difficult that's why i've kind of waited until this point to do the encryption so you guys could at least follow along with this part see how to write to files read from files all that kind of stuff there so the way that you encrypt a password we could write a manual encryption algorithm but i'm not going to do that that's a bit beyond the scope of this tutorial is we use a module by someone way smarter than all of us who knows how to do encryption and the way that we use this module is we need to install it so remember at the beginning of the video i was telling you when we imported random then hey we there's also other modules that are built by random people right uh that we can use but the thing is we have to install them because they're not by default install with python so this is where a lot of you are probably gonna have problems if you haven't done this before but open up your terminal or your command prompt depending on your operating system doesn't matter which one and just type pip install and then type criptography if i can spell it like that pip install cryptography just press enter and pray that this works you now if this doesn't work for you don't worry i'm going to show you how to fix this but for some of you that should have worked if that works for you you're good you can kind of skip probably the next minute of this video because i'm just going to explain how to fix this if this doesn't work for you try the following command okay pip 3 install cryptography pip3 install cryptography press enter see if that works if that doesn't work for you try python hyphen m pip install cryptography if that doesn't work for you try python 3 hyphen m pip install cryptography if none of those work for you this means that pip is not installed on your system pip is actually a recursive acronym that stands for pip installs packages and well what it does is it installs packages for you and by default when you install python it's not included in the installation i don't know why they don't just include it by default you have to like check a little box to say to install it so you could just reinstall python and check that box that says fix pip or you can follow the two videos that i have that are linked in the description one for mac one for windows showing you how to fix this command now they're not directly called like how to fix pip they show you how to install a module called pi game but when it comes to installing the pi game thing just instead of doing pi game do do cryptography okay so you need to install this cryptography cryptography module sorry once you have that installed we're good to go if you know what a python interpreter is and you have multiple on your system this is going to be a bit more complicated you need to make sure you're using the right python interpreter when you're running your code the one that you install cryptography into okay so at this point i'm going to assume you've successfully installed cryptography once you've done that what you need to do is import it so you're going to say from and then cryptography like that dot and you're going to type this thing called fernet uh yeah fernet like that and then you're going to import furniture now i'm looking at a cheat sheet i have because this is not something i do very often so i don't have this memorized but this is a module that is going to allow you to encrypt text essentially now the first thing that we need to do when we use this is we need to define what's known as a key so essentially what this is going to do for you and i'm not going to talk about it too much is it is going to take a string of text and using a key turn it into a completely kind of random string of text that you cannot get back to the original text from without knowing the key now this key is something that we're going to combine with our master password so imagine you have this key okay and then we have our password so the key plus this password is what we're going to use to encrypt our text now that means that if you type in the wrong password when you go to decrypt the text what's going to happen is you're going to get something that makes no sense it's not going to be the original text because you need the key plus the password to be able to get back to the original text so it's kind of key plus password plus you know text to encrypt and then that equals like random text okay and then you have random text plus key plus password equals and then text to encrypt now this is obviously like a vast simplification of what's going on here but that's kind of what we're going to do with this module so the master password in combination with the key that we're going to store will be able to encrypt and decrypt our text if you type in the wrong master password you will have a wrong decrypted text okay so what we need to do is write two functions one function that can create a key for us and one function that can store a key so i'm or sorry retrieve a key so i'm going to say define right underscore key i'm just copying this from my sheet over here that i have on my other monitor i'm going to say key is equal to furnet dot and then generate underscore key i'm going to say with open and i'm going to say key dot key this is a key file and then wb this stands for write bytes mode as key underscore file and then i'm going to say key underscore file dot write key okay so what this is doing is going to open a file it's going to create this file called key dot key in wb mode which means write in bytes you don't really have to know what that means it's just a special file format then you press as key file and or sorry as key file and then key file.right and you're going to write in this key that was generated by this thing called fernet okay that's what we're using up here that we've imported so you're going to write or sorry you're going to run this function one time and when you run this function one time it's going to create this key file for you so let me show you when i just call right key here after i input the master password it will then create this so let's run this and just type some random thing and then notice here we get this key file okay and inside of here you have this key boom good now remove the call to write key you can keep this function in if you want you don't need it anymore because you already have the key and so what i'm going to do is i'm going to comment it out so to do a multi-line comment in python use three quotation marks single or double just make sure the same on both side you do three at the start of the comment three at the end and so now i've commented out this function to make sure i don't use it again because if i use it again it's going to be a problem okay now that we've done that we need a function to load this key so in a similar way actually i'm just going to copy this i'm going to change this now to be load key like that and actually i probably shouldn't have copied that i'm just going to say return open and then this is going to be key dot key and then dot read and the mode is going to be rb mode now the thing is i need to take this here i need to say file is equal to open i'm then going to say file dot read i'm going to store this in key and then i'm going to say file dot close just to remind you that you got to close your file every time you open it okay so i'm opening the file in write or sorry read slash bytes mode so it's reading bytes i'm then going to read the file i'm then closing the file and then i'm returning the key again you don't really have to understand exactly how this is working this is just part of the decryption stuff okay so now we have the key so now that we have the key what we're going to do at the top of our program here is we're going to load it we're going to say key is equal to load underscore key which means this function needs to go above where i'm calling so this is another interesting thing let me copy this right key as well and put it up here whenever you want to use a function it needs to be defined before you use it so i couldn't have this function defined below this line if i did that that would mean that i wouldn't be able to use this function because it wasn't yet created when i tried to use it so i got to create it first then i can use it obviously okay so now that we have that what i'm gonna say is uh fur is equal to fernet i'm going to pass key now this is just initializing this kind of encryption module so you're writing fernat you're gonna pass it the key okay so actually i'm realizing i made a mistake here i just got to take this stuff and i got to put this below the master password so i'm going to say master password then key equals low key for equals for net key but then i'm going to change my key to be key is equal to load key plus the master password two bytes now you probably don't know what this means uh bytes is kind of a different way of storing information you've probably heard of bits and bytes before not the snack but like in terms of uh in terms of computing right you have bit and then you have bytes and well regardless we have our key in bytes and so we need to convert our master password into bytes so that this works so we can add this together now just like when you add two strings together that's exactly what's happening here we're taking the key which is in this file and we're just adding this master password in its bytes format to this key and then using that as the actual key okay hopefully that's clear that's like a lot of the hard stuff now encrypting stuff is really easy so what i'm going to do now is when i go ahead and write my password i'm going to first convert this password into its encrypted version so i'm going to say fur dot and then encrypt and i'm just going to surround my password with that but first i need to encode my password now the reason i need to encode this is because encoding my password will convert this into bytes and actually i'm realizing here that this is going to be better i'm going to say dot masterpassword.encode rather than dot bytes again all encode does is it takes your string turns it into bytes and so same thing here i'm taking my password i'm turning it into bytes with dotting code and then i'm encrypting it and i'm going to store that beside the name and i also realize that now i need to convert this to a string so i'm going to say string fur.encrypt pwd.encode and this will now encrypt and encode our password okay so now when we store these passwords they should be stored in an encrypted format so now the thing is we need to decrypt them when we show them so to do that is going to be literally the exact same thing we just had here so i'm going to copy this except instead of encrypt it's going to be decrypt okay so i'm going to say decrypt like that and then instead of pwd this is going to be uh pass w okay so i'm hoping this is going to work i have a feeling it's not i think i made a small mistake here but we'll worry about that when we get to it okay so now we should hopefully be storing encrypted information and reading non-encrypted information let me go in passwords and let me delete this because that is not encrypted and so that's going to give us a problem right now and let's go here and let me rerun this app okay what is the master password we now need to pick the master password that we want to use to encrypt our data so i'm just going to make mine tim makers whatever you want would you like to add a new password yes want to add account name we're going to say maybe just like facebook password will say tim is great okay would you like to add a new password or view an existing one i'm going to say view and then we get a prom it says invalid token okay so let me have a look here and see what's wrong but if we go to passwords.txt you can see that we are storing this in the file and now i've actually already determined what's wrong but uh let me just collect my thoughts and i'll be right back so i've realized the problem here and it is that we are writing in this b uh what is it i guess this is a quotation mark and then another quotation mark here and we can't be doing that so i'm actually going to clear this file so the thing here in python and this is i guess i can teach you about bytes now when you write a b and then you have uh something after this like i say b and then hello this is a byte string this is different than the string hello when you have this little b before this means bytes so the thing is we were writing in this little b and we don't want to do that because when we do that that means when we try to decrypt this it's all wrong because we have this b and this quotation mark and well just incorrect we can't be writing that into the file and so what i did before is you saw i had a string surrounding this in our add file we can't have that instead of having a string we need to decode this so i forgot that we have this thing called the code it's the opposite of encode it's going to take a bytes string and decode it to a regular string so now if we decode this all should be good and same thing here i've i had uh what do you call it string like that and that's no good i couldn't do that instead i just have to have fur.decrypt and then pass w dot encode so i'm going to take the string in because i'm writing in a string that's not bytes it's just a string and then what i'm going to do is convert that string into bytes and then decode that byte string and then that should work so now let's see if this works i'm going to rerun the program paul apologize about that mistake i'm going to say what is a master password we're going to go tim would you like to add i'm going to say add account name let's just go joe password one two three four five six seven okay now i'm gonna say view when i say view it gives me uh user joe password b and then one two three four five six seven so it's showing me this bytes thing if i want to remove this bytes thing i need to decode this so i'm going to decode like that and now if i quit here and i rerun and i type in the master password which is tim and i press view or type view none type object has no attribute decode okay what is the problem here i was printing out this oh sorry guys i added the dot decode at the end of the print statement not at the end of this fir thing so i had it like after this bracket it needs to be after this second closing bracket so let me rerun this let me type in tim let me type in view and now notice it gives me the correct password one two three four five six seven however if i go to passwords.txt you can't see the correct password right it's storing this like a random string of gibberish that really like means absolutely nothing and so if i actually quit this let me just clear the screen and rerun uh let's go to password manager let's go to run and i type in the incorrect password i'll just type tim2 and i type view notice it still gives me the correct password okay it's interesting that's giving me the correct password it shouldn't be giving me the correct password so let me look at this for a second okay so i'm going to apologize here because i made a mistake i thought that we were going to be able to use a master password in combination with our key and just do some kind of like hackish manipulation to get this to work unfortunately it looks like doing that's way more complicated than i thought i just was looking on the internet right now in fact i'll bring up the documentation that i found um so we're using this thing called fernet right and there's a way to use passwords with fernet but i mean you guys can kind of read this here it's a little bit complicated and i don't really want to put you guys through all of this because this is just a beginner project so i will leave a link to this documentation in the description if you'd like to have a look at how to do this but for now i think i'm going to end the video here so i'm just going to remove this master password feature obviously that's pretty important right like you would want to have a master password so i really do apologize about that but if you do this now and you just use the key as you used it before um everything will still work so right now if you run the program and i'll show you and we don't have a master pass says would you like to add a password to view by type view it gives me the correct passwords right even though we used technically a different key that had the addition of our master password to kind of encrypt them previously this still works so anyways that is where i'm going to leave it again i'll leave a link to that documentation that shows you how to implement the master password in the description just too complicated for me to feel comfortable going through it in this kind of beginner tutorial all right so with that said i hope you guys enjoyed these five mini python projects i hope they kind of gave you something to work on maybe taught you a little bit about python and at minimum you know gave you a cool project that you can kind of extend and add on to if you guys like these type of videos please make sure to leave a like let me know what you want to see in the future in the comments down below and i will see you in another youtube [Music] video you
Info
Channel: Tech With Tim
Views: 230,793
Rating: 4.9633737 out of 5
Keywords: tech with tim, python, python programming, python projects, python beginner projects, python projects for beginners, python tutorial, python for beginners, python tutorial for beginners, mini projects, mini python projects, mini python projects for beginners, quiz game, number guessing game, rock paper scissors, choose your own adventure, password managers, VSCode, vscode python, vscode python setup, 5 mini projects, project tutorial, vscode setup, beginner projects, projects
Id: DLn3jOsNRVE
Channel Id: undefined
Length: 101min 7sec (6067 seconds)
Published: Thu Jun 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.