The Ultimate 25 Minute Python Project!

Tech With Tim · Intermediate ·💻 AI-Assisted Coding ·3y ago

Key Takeaways

The video demonstrates a 25-minute Python project, building the Mastermind game, which involves guessing a four-color or four-digit code generated by the computer. The project showcases various Python features, including proper program layout, variable naming, function creation, and code security using Contrast Security's CodeSec.

Full Transcript

foreign [Music] you're looking for a quick python project that you can use to practice your skills and learn about some new python features then this video is for you what we're going to do here is build the popular game known as Mastermind this takes about 75 lines of code I'd rank a kind of intermediate difficulty and it's going to teach you a lot about python especially if you're kind of a beginner or getting into those intermediate stages I'm also going to show you how to properly lay out a program even though this is small you're going to see how an experienced programmer names their variables creates the different functions and kind of just sets up the general structure of the program which is some valuable information so with that said let me give you a quick demo of the game so you understand how it works and then we'll get into building the project foreign [Music] code open here this is where I'm going to run and write all of the code for this project if you don't already have it I'd recommend you download it from the link in the description regardless you can still use the python idle or whatever your preferred environment is this is just what I'm using for this video now I've already written the project just so I can demo it for you so let's have a quick look at how it works so this game is Mastermind the general idea is you're going to have a player in this case that's going to be us who has to guess a four color or kind of four digit code so in this case the computer is going to be the one generating the code typically you play with two people and one person is kind of the code maker okay so I've actually printed out what the code is here just so that we can see it and kind of compare to it as we go through this demo typically you wouldn't show the user what the code is so it says welcome to Mastermind attempt to guess the four digit code you have 10 tries the colors that can make up the code are red green blue yellow white and orange so you're going to make a guess of four colors try to guess what the code is I always like to start with just one color so red red red red okay so now we can see that we have zero colors that are in the correct position and zero colors that are in the code that are in the incorrect position so now I'm going to go with say white white white okay zero again now obviously we can see the code so let's speed this up and go with four yellows now it tells me that I have two colors here that are in the correct position and zero that are in the incorrect position that's because these two y's are in the correct position if there was another y but I didn't have it in the correct position then this would give me a one okay so now we have y y so let's do g g y y now it tells me I have three in the correct position but I don't know which those are necessarily so I'm going to go with maybe W actually not W let's go with like o uh actually G o y okay and now it tells me I have two in the correct position one in the incorrect position the reason I'm getting incorrect is because I have a green here so I've put that there it's in the code but it's in the incorrect position so hopefully that makes sense that's why it's showing it to me and now we can just go ahead and guess the code all right and there we go we guessed the code in six tries there you go that is The Mastermind game it'll make more sense as we go through the tutorial with that said let's dive into the project after a quick word from our sponsor thanks to contrast security if you're sponsoring this video contrast security provides a world leading code security platform that is purposely built for developers to get secure code moving swiftly contrast also makes security testing available to all Developers for free with their code SEC code SEC is the fastest and most accurate scanner and can be up and running in less than five minutes with codesec you can scan secure and ship your code in minutes for free while finding seventy percent more critical vulnerabilities and actionable results with six times more true positives in just seconds start by installing contrast authenticate and then with one command audit your entire code base and get detailed information about security vulnerabilities in seconds for free get started with contrast security and start scanning your code with codesec today for free by clicking the link in the description thanks to contrast security for sponsoring this video [Music] alright so let's go ahead and start building this project now I'm just going to spend a minute here and discuss what it is that we actually need in terms of the sub components for this project that we can build those one at a time and then kind of tie them all together at the end this is a good practice whenever you're writing code you want to think about it for a second before you just dive into the code so the first thing we need to do is we need to generate a random code this is going to be four colors or four digits whatever you want to refer to it as and they have to use kind of the six colors that are part of the game okay step one generate a code next we need a way for a user to guess what the code is we want to make sure that what they're guessing is valid so if they guess only three colors or if they guess five colors or if they guess a color that doesn't exist we need to give them some feedback and tell them hey that's invalid make another guess now after that we need to compare their guess to what the real code is to do that we need to determine the number of correct colors the ones that are in the correct position and the number of colors that are in the code that are in the incorrect position so we can give them that output this is probably the most challenging part don't worry I'll walk you through how we do that after that we kind of just need to tie the game together right we need to do 10 guesses we need to constantly give the output saying this is how many correct this is how many are incorrect we need to determine if the user got the correct code and we need to determine if the user lost the game that's really all we have to do what I like to start doing is building out each of the individual components and then at the end I kind of tie them together so let's go ahead and do that step one is going to be to generate our code so since we're going to be generating a random code I'm going to start by importing the random module and now I'm going to Define all of the colors that we're going to be using so I'm going to say colors in all capitals the reason I'm doing capitals is because this is a constant and that's kind of a convention in Python it's a variable that's not going to change so for my colors I'm just going to refer to my cheat sheet here for what we have we have red green blue yellow white and orange I don't know if these are the real colors that are used in the game but these are just the ones that we're going to use and in the real game it has six colors that's why I'm doing six okay next I'm just going to Define a variable called tries I like to put all my constants at the top of the program just they're easy to find to find story and this is the number of tries you get in the real game so I'm doing kind of the same thing 10 tries okay next we're going to write a function which is going to generate the code for us so I'm going to say Define generate underscore code like that now I'm actually going to make another constant called code length and make that equal to 4 just because if I wanted to make this game harder maybe I make the code length 5 and I increase the tries you can kind of make this Dynamic by changing these different values assuming that we use them properly throughout the program okay so for generating the code we need to create in this case a list the list is going to contain all of the elements in our code and we're going to have four elements so I'm going to say my code is equal to an empty list inside of my function by the way a function is just a reusable block of code that you can call multiple times if you're unfamiliar with it it's just something that we can use later on so I wanted to mention that okay now that we have our code list we're going to insert four random colors into that list to do that I'm going to say 4 underscore in range and then I'm going to go the range of my code underscore length and this underscore is essentially a placeholder for what you would normally put here which is like I or X or some value in this case I don't care about what what iteration I'm on so I can just put an underscore meaning I don't really care about what that variable is it's kind of like a placeholder it's known as an anonymous variable okay what I'm going to do now is select a random element from this colors list the way that I do that is I say color is equal to random dot choice and then of colors okay that's going to randomly select a color from this colors list I'm then going to say code dot append and then I'm going to append that color into my code this for Loop is going to run four times which is our code length it's going to randomly select four colors and then put that into our code okay then I can return the code from this function which is what I can access when I call this function so the way this would work is I'd say code is equal to generate code then it's going to give me a list of four random colors perfect okay now we have a way to generate the code the next thing we need to do is allow the user to guess the code so I'm going to make a function for that I'm going to call this guess underscore code all right so for guessing the code we're going to have to ask them to enter some input so I'm going to say their guess is equal to input this allows the user to type in the console and I'm going to say guess colon like that now what I'm going to do is convert their guess to all uppercases just so that if they type like a lowercase G here we're going to convert that to an uppercase G so when we compare it to the real code we don't get an issue if they typed in lowercase then what I'm going to do is use the dot split function now I'm going to put a space inside of here and what this is going to do is it's going to take all of the input and turn it into a list based on all of the spaces so for example if I have GGG like that and I have spaces in between them then what that dot split function is going to do is use all of the spaces to create elements in a list so I'm going to get a list that has four G's inside of it and it's going to remove all of those spaces for me hopefully that makes a bit of sense but it's just going to take all the spaces and kind of grab a all of my individual colors using the spaces as what's known as a delimiter if you wanted to do it with commas you change this to a comma and then you would expect an input like this if I could type that correctly okay hopefully that makes a bit of sense but that's what dot split does all right now that we have a list containing all of the different colors the user inputted we're going to check the length of that list and see if it's equal to four that will tell us if they inputted four colors or not so I'm going to say if guess or if the Len of guess does not equal 4 then I am going to print you must guess four colors okay and really rather than doing four I'm going to change this to be the code length so that later on if we want we can change what the code length is and then here I'm going to use an F string and do code length as well and F string is available in Python 3.7 and above and allows you to embed variables directly inside of a string using curly braces you put an F before the string and then curly braces like this and you can insert the variable directly in the string now while I'm at this I'm going to put all of this inside of a while loop because if they input an invalid code then I need them to go through the loop again and give me another guess so here inside of this if statement I'm gonna have a continue and what continue we'll do is just bring me to the top of this Loop okay so if the length of the guess is not equal to code length we print this and then they're gonna have to guess again and keep doing that until they give us four digits lastly I need to make sure all of the colors that they guess are actually in the colors list so to do that I'm going to say four color in guess which is now a list and I'm going to say if color not in colors then what I'm going to do is say print another F string invalid color and then we'll put what the color was and we'll say try again okay then what we can do is we can break out of this then what we can do is we can put an else statement here and we can say break now I'm going to explain what this means uh but here I'm just going to return my guess okay I think that's all we need but let's have a look at it so essentially what I'm doing here is I'm looping through every single color in the user's guess because now this is a list and it contains all of the individual colors that were separated by spaces I'm going to say if any of the colors are not in this colors list so that's all of the valid colors here then I'm going to tell them hey this is invalid please try again and I'm going to break out of this for Loop now when I break out of this for Loop what that's going to do is essentially just make it so I continue with this while loop now what this else statement does here is essentially check if I didn't break out of the for Loop so what I'm doing is I'm saying if I never encountered this break keyword inside of the for Loop this else statement is going to run that means that all of the colors they had were inside of the colors list because if this never happened and I never breaked that means every single color I had was valid okay now that brings me into the else here because I didn't break so that's what the else is doing telling you if you didn't break out of the for Loop and then from this else I'm breaking out of the while loop so this is while true so the only time I get out of this Wallop is if I pass through this for Loop and all of the colors are valid hopefully that makes sense but this is what you can do on a for loop it's actually really cool syntax allows me to determine if I broke or not I didn't break so I'm going to break here out of the while loop and then return whatever the guess is that the user gave me okay so that is guess code now that we have guest code the next thing we're going to do is say check code okay and here we're going to take the guess and the real underscore code and we're going to check how many are correct alright so this is a little bit more complicated and what this involves is essentially looking through every single color in our code and first of all determining if the guessing color matches the real code colors that's the first thing that we need to do that's the easy part now we can determine how many color others are in the correct position once we do that though the more difficult part is determining the colors that are in the code but that are not in the correct position the reason for that is that we could have colors that are in the correct position and we don't want to count those as being in the code but being in the incorrect position again I know this seems a little bit weird but there's a few things we have to do here so just try to follow along with me and I'll explain it as we go kind of Step by Step but step one is going to be check the correct position then we need to check the colors that are in the incorrect position and it's important we do it in that order to make sure we don't double count a color so what I'm going to do here is I'm going to say color underscore count or counts is equal to a dictionary and then I'm going to say correct underscore pause is equal to zero an incorrect underscore pause is equal to zero to keep track of the colors in the correct position and the incorrect position all right first thing I'm going to do here is I'm going to say four color in the real underscore code and I'm going to keep track of the counts of all all of the colors so I want to know do I have two Reds do I have one yellow do I have one green I'm going to store that inside of the color counts dictionary so I'm going to have something like green and then two which means we have two greens inside of the code the reason I want that is so that later when I'm checking the incorrect colors I can make sure that I'm not using colors that were in the correct position or counting those towards my incorrect you'll see what I mean but just try to follow along all right so I'm saying four color in real code I'm going to say if the color is not in the color counts which means the key right so like key meaning like this green and sorry I need to get out of this uh if the key is not inside of the dictionary then I want to add it so to add it I'm going to set color underscore counts and then this is going to be at color is equal to zero and then no matter what let me just close this I'm going to say color counts at color plus equals one so again I'm looping through all of the colors in my real code I'm saying if this color is not a key in the dictionary then we need to add it so I'm setting that key to zero the reason for that is that if I try to increment a key that doesn't exist I'm going to get an error so if the color is not there I add it in and then no matter what I increment the count for that color in my dictionary if you're unfamiliar with dictionaries I have a ton of python tutorials I think I have one specifically on dictionaries you can check that out from my channel all right so now that we have the counts what I want to do is find all of the colors that are in the correct position so I'm going to say four guess underscore color real underscore color in and then I'm going to use a fancy function called zip and I'm going to zip the guess and the real code now what this ZIP function does is the following it's going to take my two arguments and it's going to combine them into tuples so if I have guess and real code let's say my guess is equal to like G and R and my real is equal to like w and yellow then what the zip is going to give me is the following it's going to give me a list that has if we could do this properly has G and W and has r and Y okay so it's going to essentially combine the elements at the same position into tuples and then give me a list of that the reason for that or the reason I'm doing that is so I can easily compare them because now I have a tuple and then what I'm doing here is decomposing the Tuple into its two individual elements so I'm getting the real color or sorry the guess color and then the real color which are the two elements from my Tuple hopefully that makes a bit of sense but that's how the zip function works so now that I have that I can compare them so I'm going to say if the guess color is equal to the real color because again these are at the same positions in our codes then what I'm going to do is say my correct underscore positions plus equals 1 and I'm going to decrease the count from my color counts dictionary so I'm going to say color counts at and then we can use either the real color or the guess color because of the same it doesn't matter I guess color minus equals one the reason I'm doing this is I want to mark that this color that I just found is in the correct position so I don't want to potentially count that when I'm counting the colors that are in the incorrect position so you'll see how it works when I start looking at the incorrect position but I'm essentially getting rid of that color from the count to make sure that I don't use it again and count it as an incorrect color because it's not incorrect it's in the correct position so now I need to kind of remove it and forget about it because I've already handled it okay now we've done all the correct others pretty easy straightforward with the for Loop now we need to do the same thing but for the incorrect colors so again I'm going to copy this kind of for Loop structure and now I'm going to say if the guess underscore color is in the color underscore counts I could use the real code or color counts it doesn't matter if I use color counts it's checking if I have a key that exists in color counts so it's the same thing and color underscore counts at the guess underscore color is greater than zero all right sorry for the cut here but I want to quickly demo to you an example of a real code versus our guess and what we would tell the user and kind of why we're writing the code the way we are so here we have our real code which is green orange orange and then we have our guest which is green green ww in this case what we tell the user is that they have one color in the correct position that's it they have zero colors in the incorrect position the reason for that is that the greens here are in the correct position so that means if I see another green I'm not going to say that's in the incorrect position because there's not another Green in the code that it doesn't match with right here we have these two greens they kind of cancel each other out and now as we go through the rest of the guess and we compare that to the real one we don't say to the user hey this is in the incorrect position we would only do that if there was another Green in the code so hopefully that makes a bit of sense now the reason we're going through this whole process of keeping track of the count is so that as we go through here and we see this green we make sure we don't tell the user it's in the incorrect position just because a green existed before it this green since it matched up with a green that was in the correct position shouldn't be looked at anymore hence why we're subtracting it from this color count hopefully that makes a little bit of sense but that's why we're keeping track of the counts and that's why we need to First go through and keep track or find all of the colors in the correct position then only after we do that and we determine how many other colors are left to kind of match up we go through this okay you can mess around with a few different examples and try kind of changing the code around and you'll see what I mean but hopefully that gave you somewhat of idea of why we're writing the code the way we are anyways what I'm doing here is I'm saying if the guess color is in the color counts so if the key exists and the key there is greater than zero meaning we still have a color that we can use to kind of match with this one then we're going to say the incorrect position plus equals one and again we're going to subtract our color counts so color counts at the guess underscore color minus equals one because now we found one color that's in the incorrect position so we want to make sure that we're not going to say another one is in the incorrect position if there was only say one color to match with so we need to every time we tell the user one's in the correct position or in the incorrect position kind of eliminate that color from the code to be matching against as we continue kind of doing this check and matching all right now that we have this we should know how many correct and incorrect positions there are so we can return both of those pieces of information with correct pause incorrect pause separated by a comma we can just get both those pieces of information out of the function okay now we have the three main components of our game we now need to link them together with some main kind of game logic so I'm going to make another function called game inside of here I'm going to start by generating the code so I'm going to say the code is equal to generate code like that all right that gives us our code next I'm going to have a for Loop and I'm going to say 4 actually attempts in range and then this is going to be 1 comma and then tries plus one Okay the reason I'm doing this is because the range function will go up to but not include the last value here so I'm going to just start at 1 and go to in this case 11 which means I'm going to go from 1 to 10 so that I have kind of the correct try number hopefully that makes a bit of sense but you'll see why I'm doing that in a second all right then the first thing we need to ask the user to do is guess the code so I'm going to say guess is equal to guess underscore code very simple you can see how our functions are helpful now this is going to get the user's guess once we get the user's guess we want to compare that to the code so I'm going to say correct pause incorrect pause is equal to and then what I call this check underscore code we're going to pass the guess and the code then we need to tell the user some output so we're going to say print F string and we're going to say correct positions and then this is going to be correct underscore pause I'm going to do a pipe and I'm going to say incorrect positions and then incorrect underscore pause okay uh very good and then it's going to ask them to guess again so that's really all we need uh although I want to check if the user won or if they got the code so actually before I do this I'm going to say if correct underscore positions is equal to the code underscore length then I'm going to print U guessed the code in and we'll do an F string here as well and then this is going to be attempts and then tries like that and then I'm gonna break out of the for Loops so I don't tell them this after I already told them that they guessed the code all right then we're gonna have an else statement here and I'm going to say print you ran out of tries the code was and then I'm going to do asterisks and then the code now what this will do is just print out every individual element from my list so essentially what asterisk does is just takes every individual element and kind of passes it to the print function which means the code is going to be printed space separated from this variable you'll see what I mean if we actually encounter this case but it's kind of a cool thing you can use to print out a list and have it look decent with kind of space separated values all right I think that's all we need I'm just looking here and making sure this is okay and yeah I think that's it lastly what we can do is just print kind of some intro text here so we're going to say print you know welcome to Mastermind you have and then this is going to be F string tries to guess the code dot dot dot and I'm going to say print and then the valid colors are and then again asterisks and then this time we're going to do colors okay lastly we need to call this game function so I'm going to say if underscore underscore name equals underscore underscore main then game this will run the game one time if you wanted to run the game multiple times you could put this inside of a while loop and then continue asking the user do you want to play again if they enter yes you run the game again by calling the function if they enter no then you just quit and stop running the game if you're wondering what this line is here this just makes sure that we're actually directly running this python file because you can import python files and if you were to import this file without having this line like let's say you just had game here then it would run the game when maybe you don't want to do that you just want to be using some of the functions that are inside of our code so this just make sure you're directly running the python file all right I think that's going to be it for the game I want to quickly run through the code then we'll just make sure it's working so let's go through this first we're printing you know our welcome messages we generate the code and then for the number of attempts that we have we're going to do this we say the guess is equal to guess code okay that just gets the user to guess our code we then compare the code to the real code and then we tell them or sorry we check first if they actually guessed the code if they did guess the code and we can determine that by seeing if their correct positions equal the code length we tell them they guessed it in this number of attempts and then we break out of this for Loop otherwise we tell them the number of correct positions and incorrect positions they had in their guess if we get to this else statement here what that means is we never broke out of this for Loop which means the user never guessed the code and the for Loop ended so they ran out of tries we tell them you ran out of tries and then we tell them what the code was pretty straightforward that is game we have check code we already ran through that guest code and generate code and then all of our constants at the top of the program and you can see kind of how this program is well laid out it's very easy to understand what's going on foreign [Music] our code and see if this works okay so I'm going to run it here we have welcome to Mastermind you have 10 this should say tries to guess the code the valid colors are the following I always like to start with just one color okay so correct position one incorrect position zero okay now let's try this uh correct positions two okay that tells me the Y is in the correct position or sorry not the Y the R so I'm gonna go r y w w okay so now I know my Y is somewhere else so I'm gonna go r Let's Go Blue y w two correct positions okay let's go r o y o and we guessed it okay nice got the code that was actually pretty lucky there that we guessed that on try five now of course we could run the code multiple times test out all of the different cases but I think for now that's it and our code is working so with that said guys I think I'm gonna wrap up the video here I hope that this was helpful I'll zoom out a bit just so you can kind of read most the code I can kind of slowly scroll through it in case you want to pause and see where maybe you made a mistake if you guys like these kind of short quick python projects let me know in the comments down below and I'm happy to make more with that said I'll wrap it up here hope you guys enjoyed if you did make sure to leave a like subscribe to the channel and I will see you in the next one [Music]

Original Description

Today, I'll be showing you a quick Python project that you can work on to hone and practice your skills, while learning some new Python features! 💻 Start scanning your code with CodeSec by Contrast Security for free! https://bit.ly/3VLR0P0 💻 Master Blockchain and Web 3.0 development today by using BlockchainExpert: https://algoexpert.io/blockchain - use code "tim" for a discount! 💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! https://programmingexpert.io/tim - use code "tim" for a discount! ⭐️ Timestamps ⭐️ 00:00 | The Ultimate Python Project 01:00 | Project Demo 04:20 | Project Walkthrough 27:28 | Running the Project! ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear: https://www.techwithtim.net/gear/ 💵 One-Time Donations: https://www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8 💰 Patreon: https://www.patreon.com/techwithtim ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ ⭐️ Tags ⭐️ - Tech With Tim - Python Development - Coding ⭐️ Hashtags ⭐️ #techwithtim #developer #python #howtolearncoding
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches you how to build a Mastermind game using Python, covering topics such as program layout, variable naming, function creation, and code security. You'll learn how to use Contrast Security's CodeSec to scan your code for vulnerabilities and improve your coding skills.

Key Takeaways
  1. Import the random module
  2. Define constants for colors and tries
  3. Define a function to generate a random code
  4. Use a for loop to insert 4 random colors into the list
  5. Use input to get user input and convert it to a list of colors using split
  6. Check if the length of the user's input is equal to 4
  7. Use F string to embed variables in strings
  8. Put input code inside while loop for repeated input
  9. Use for loop to check each color in user input list
  10. Use else statement to check if all colors are valid and break out of loop
💡 Using CodeSec to scan your code for vulnerabilities can help improve your coding skills and identify potential security risks.

Related AI Lessons

Chapters (4)

| The Ultimate Python Project
1:00 | Project Demo
4:20 | Project Walkthrough
27:28 | Running the Project!
Up next
Azure Security Priorities for 2026: Identity, Governance, AI Security & Zero Trust
Valto Microsoft Specialists
Watch →