📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Learn Python With 5 Projects - From Beginner to Advanced

Tech With Tim2:51:09

Transcription

Today, I'll be teaching you Python by walking you through five projects. We'll start at the very beginner level and then slowly work our way up, getting more and more complicated as we learn more theory and implement new features.

Now, this video is designed for complete beginners. I'm going to explain everything as if you've never used Python before, and I'm even going to show you how to install Python and set up your editor. We'll start really slow, working through things like print statements, input statements, and how to do something like an if statement. We'll cover all of the absolute basics and then immediately apply that into a project.

This way, you don't just learn about the topics, but you actually see how they're used in the real world, and you have some real-world projects that you can adjust and play with afterwards. I find this is definitely one of the best ways to learn. People not only get the theory but also get the real-world application, which is often what's missing in a lot of different tutorials.

Now, with that said, we're going to be building five projects. Let me walk you through what those projects are. If you find them going too slow for you, feel free to increase the playback speed of the video or skip forward using the timestamps in the video player to the next project.

The five projects are as follows:

1. We're going to start with a Choose Your Own Adventure game. This is kind of a multiple-path game that allows us to go down different avenues based on prior decisions that we make.

2. We're then going to build a text-based calculator.

3. We'll then build a contact management system.

4. After that, we'll build Tic-Tac-Toe with an AI that can play against us.

5. Finally, we're going to build an automated quiz generator.

Obviously, as we go through here, it gets more and more complicated. Don't feel like you need to finish this video in one day. Take your time, code along with me, pause the video, come back to it another day. Really use this as a great learning resource.

Anyways, let's get into it. But I quickly want to talk to you about a great free resource that you can check out, especially if you're new to Python.

Now, this resource is called the Introduction to Python, and it's provided by our friends at HubSpot. Whether you're a marketer, developer, or data analyst, this guide is perfect for you. It covers what Python is, key methods and functions, coding standards and best practices, advanced Python techniques, and so much more.

I've included a link in the description where you can check it out completely for free. Python is an incredibly popular language and a great skill to learn, even if you don't plan to become a professional developer. This guide will show you the amazing potential you can unlock with just a basic understanding of Python.

Now, personally, my favorite part of this guide is the great overview of Python object-oriented programming concepts, which acts as a great reference or cheat sheet. HubSpot offers this guide and tons of others completely for free, so a massive thank you to them for that and for sponsoring this video. Make sure to check it out for free from the link in the description.

Alright, so I'm on the computer now, and I'm going to go ahead and get started. If you already have Python installed on your machine and you know how to write Python code, feel free to fast forward to the timestamp for the first project. This is just for people that are really complete beginners and are just getting started.

The first thing we need to do in order to write Python code is have Python installed on our system. To do that, we're going to go to the Python website, which is simply python.org, and we're going to go to the download section.

From here, you're going to download the version appropriate for your operating system. It should show it to you by default, but you can click through Windows, Mac OS, or potentially Linux. In our case, I'm on Mac, so I'm just going to download the newest version of Python for Mac. If you're on Windows, then you would download this for Windows.

This will then go to your downloads folder or wherever you decide to download it. What you need to do is simply double-click it, run the installer, and just run through it with all of the default options. You don't need to change anything.

Now, once you've done that, we want to test to make sure Python is working properly. To do that, it's going to differ depending on your operating system, but it is similar. If you're on Mac or Linux, what you want to do is go to the Spotlight search, which is the little search icon up here in the top right-hand corner, and search for a program called Terminal.

If you are on Windows, you're going to be searching for a program called CMD or Command Prompt. The way you do that is by going to the bottom where you have that kind of Windows search bar and typing CMD.

Okay, so you want to open up Command Prompt or open up Terminal, depending on your operating system, and you simply want to type the following: python3.

Now, when you type python3, you should see something pop up. What you want to look for here is this Python version, and just make sure it matches the version that you used when you downloaded Python.

In my case, if you go to downloads, it's Python 3.12. It might be further when you're watching this video. You want to make sure this version matches.

Now, you can ignore my version because I have a different setup on my computer, but the point is you want that to match.

Now, this is the Python shell. We're not going to use this right now. This is simply for testing. To get out of this, you can type quit with two sets of parentheses, or you can just close the terminal.

If you're on Windows and for some reason that command did not work, what you can try is typing python. Now, when you type python, you may or may not get the same output, but again, you want to check for this version and make sure that you have Python version 3 something.

In some cases, there's actually Python version 2. You don't want to be using Python version 2; you want to be using Python version 3 something.

Okay, so ideally that would have helped you install Python. There are some things that can go wrong here, but for most of you, Python should be installed.

Now, once Python is installed, what we want to do is actually download an editor where we can write Python code. Truthfully, it doesn't matter where you write your code, but in order to follow along with this video, it's going to be easiest if we download and install VS Code.

Now, VS Code is a very popular free code editor. So what we're going to do is just search for VS Code on Google and just go to the first link, which is code.visualstudio.com.

Now, this is actually made by Microsoft. Fun fact: I actually worked on VS Code when I worked at Microsoft, specifically the Python extension, which we'll be looking at in a minute.

So now what we're going to do is download VS Code. Again, download it for Mac, download it for Windows, download it for Linux, whatever operating system you're on. Go to your downloads folder or wherever you downloaded it and double-click to run the installer.

So now ideally, you should have VS Code installed on your system. Now, what we want to do is open Visual Studio Code. If you're on Mac, in order to do that, you can go to the Spotlight search and search for Visual Studio Code and simply open it. It should be in your Applications folder.

If you are on Windows, you're going to again go to that Windows search in the bottom left corner, search for VS Code or Visual Studio Code, and then open that application. You can also just do it by opening up this big app view if you're on Mac, searching for VS Code, and then pressing on it.

Okay, now when you open VS Code, I'm just going to make this full screen. By the way, you may see a potentially different window than what I see here. That's fine. You can just close this welcome tab like that.

Now, in VS Code, there are a few things we want to be aware of. First of all, this little icon here that looks like a file is the main place where we're going to work, and this allows us to view the different files that we have in our project.

The first thing you want to do is open up a folder. You can do that by pressing "Open Folder" here from the Explorer, or if you don't see that option, you can go to File and then Open Folder.

So let's do that, and let's just make a new folder that we're going to do this work in. I'm going to go to Desktop and create a new folder.

Now you see "New Folder." I'm just going to call this "Python Projects." You can call this anything that you want, but don't use any special characters or anything crazy; just use normal letters.

Okay, so we see "Python Projects" was created. Now we're going to open it. Again, just replicate the same thing on Windows; it should be very similar to do this.

Okay, so now you can see that we're kind of open here in "Python Projects." Same thing, we don't need this welcome screen; we can just close that, and this is where we can start writing our Python code.

Now, in order to write Python code, we need to make a Python file. A Python file simply dictates that the type of language we're writing is Python.

To make a new file inside of this folder, there are a few different ways, but the easiest is to find this new file icon right here. It looks like a file with a plus sign, and then it'll pop up this little text input where you can give this file a name.

Now, I'm going to call this "project1.py." Notice when I add the .py, which stands for Python, we get a little Python logo popping up. Don't worry if you don't see that logo, but the point is press enter here to make a new file.

Make sure whatever you call it, you don't have any spaces in the name or anything; just probably name it exactly what I have: "project1.py." This is a Python file, and now we are able to actually write Python code inside of here, and it will be detected as Python code.

Okay, now another way to make this new file is to go to File and then press on New File. When you go here, you can select Python File, and it'll make a new file for you. If you go to File and then you press on Save or you press on Save As, it will pop up this kind of box here, text input, and you can give the file a name.

So there are a lot of different ways to make a new file. If you're having trouble, go up here to File, New File, select the Python file, or name something .py, and it should get added to this folder.

I know I'm going slow here; if you want to fast forward, feel free. I just want to make sure everyone doesn't get stuck.

Okay, so now that we have our Python file, we want to be able to run this file. The easiest way to do this is to install the Python extension.

In VS Code, this is a customizable code editor. This is the place where we write our code. We can customize it by changing settings. For example, yours might look different than mine because I have a theme on my editor. You can add different extensions; you can really make this work the best for you.

So what we're going to do is go to this extensions tab. It looks like some puzzle pieces here. You can see where my mouse is, and we're going to search for Python.

Okay, now you're going to see this very popular extension. You'll know it's correct when you see that it has hundreds of millions of downloads. Simply install this extension. You shouldn't need to restart VS Code or anything; it should just install, and this is what's going to allow us to actually execute our Python code.

Okay, so now that we have the Python extension installed, it might ask you for some kind of prompt or input. Just follow along with whatever it says with the default options.

Now we're just going to write a single line of code just to test things out, so bear with me. We're going to type print with two sets of parentheses, and then I'm going to put inside of a set of quotation marks "Hello, World."

Okay, this is simply testing. You don't need to know what this is doing; just type exactly what you see on the screen. Keep in mind, if you have even the slightest change, like you add an extra space or you're missing a quotation mark, then yours isn't going to work.

When we code, we have to be really specific. We have to make sure that everything is exact. Even the smallest mistake will cause your program to fail, and that's really a frustration point for a lot of beginners. So just make sure you're being really thorough and reading through all of the lines.

Okay, so what we're going to do now to attempt to run this is the following. First, you may see this plus icon in the top right-hand corner. If you see that icon, that's great; your life is really easy, and you can just press on this.

When you press on this, you're going to see some terminal pop up at the bottom of your screen. This is similar to the one we were using before to test our Python installation, and you should see some output like "Hello, World." You might see a bunch of other stuff as well; you can ignore that. You just want to look for the output from your code.

Okay, so that should work. If you're on Windows, you're going to see a command prompt pop up, and if you want to close this, you can press on the x button. If you want to access it again, you can put your mouse at the bottom of the screen. You should see a little up arrow, and you can just drag this from the bottom of the editor.

Now, if you want to clear the output, you can type clear, or if you are on Windows, I believe this might be CLS to clear, but on Mac, it is clear.

Quick cut here; just want to let you know that in order to run your code, you do need to save it. So once you start doing any changes in your file, and I know this looks different than what you saw before, you'll see that this white dot pops up on your project name or on your file name.

Now, if you see that white dot, it means it's not saved yet. So if you try to run the code, it's going to run the old version of your file, not the new one, because you haven't yet saved it.

To save it, you can go to File and then Save, or what I always do is hit Ctrl + S or Command + S on your keyboard. When you do that, you'll see that the white dot goes away, and now you're safe to run your code, and it will be the updated version.

Okay, now for those of you that don't see this plus icon popping up, we're going to try the following. You're going to hit Command + Shift + P on your keyboard. Now, this is going to open the command palette. If you're on Windows, this is going to be Control + Shift + P.

From here, you're going to type Python, and then you should be able to type Select Interpreter. You don't need to type the whole thing, and you can see that it's popping up here.

So what we're going to do is select our Python interpreter. The interpreter is the thing that actually executes and runs our Python code. We don't need to get into all the semantics of it; the point is sometimes this isn't selected, and you might need to manually select it.

So we're going to press on this, and you should see some options, or for you, probably just one option popping up. So what you want to do here is simply select any version of Python you see. For most of you, you're just going to select the newest version.

Notice I have a lot; you can kind of ignore mine. I just have a bit more of an advanced setup than many of you will have on your computer. So make sure you select a Python version, and if nothing's popping up here, I'm going to recommend that you install Python again because you should really be able to see this.

So in my case, I'll just select Python 3.9.6. Okay, so now that I've selected that, we should see the plus sign popping up. If for some reason it's not popping up, don't worry; we can still run our code.

In order to run our code, we can open up our terminal, and we can run it manually. Now, to run it manually, you simply type python or python3. Try both of them because sometimes it's Python, and sometimes it's Python3, and then the name of the Python file that you created.

In my case, the name of the file is project1.py. Okay, if you named your file like tim.py, then you would put tim.py here. Make sure you have a space.

So we're going to hit enter, and then if the file exists, it will run it. If not, you'll see something like this where it says no such file or directory. Make sure you read these messages because they can be super helpful.

Okay, so I'm going to run project1.py, and then you'll see we get the output of "Hello, World." Okay, so that's another way to manually run your code.

Again, if python didn't work or python3 didn't work, try just python, and hopefully, that'll work as well.

Okay, so let's close that, and now let's move into our first project where we're actually going to learn Python.

So the first project we have is a Choose Your Own Adventure game. Now, we can get into the details of that project, but I first need to share with you a few basics of Python so we know how to even begin this.

So what I want to start by showing you is the print function. This is the first thing that everyone learns. Now, print is just something that outputs something to your console. The console is just simply where we're executing the code.

So in this case, it looks like this, but it can look slightly different. The point is it's kind of giving us some output; it's sharing something with the user.

Now, in order to print something, if you just want to print like a normal piece of text, you need to surround that in double quotation marks or single quotation marks. If I try to print something like hello and I don't surround it with that and I run this, you'll see that it gives me an error and it says something like "hello is not defined."

Okay, so make sure that if you do want to print something out, you surround this with single or double quotation marks.

Now, also notice the rest of the syntax here. I wrote print, I wrote an open parenthesis, I wrote a close parenthesis, and then inside of here is kind of what I want to print out. Great, so that's the basics of the print function.

Now, if you want to print multiple things, you could do something like this. Notice it doesn't matter if it's single or double quotation marks; both work. Although when I save it, it kind of automatically formats it for me; you can ignore that.

But if you separate things with commas, they'll print out, but they'll automatically be separated with a space. So if I run this, you'll see that we get "Hello space World."

Okay, so whenever you add the comma, it allows you to print multiple things; they'll just automatically be separated by a space. You also could just remove this and add a space yourself if you wanted to print out what's known as a string.

Okay, that's the print function—super straightforward. Try it out on your own computer; make sure it works.

Now, you can also call the print function multiple times. If you called this another time and I said, "You know, Tim, what happened?" you'll see that things will show up on new lines.

So by default, after the print function runs, it actually adds a special character to the terminal that tells it to go to the next line. So when we print again, we automatically will print on the next line, not on the same line.

If you wanted to print things on the same line, you would need to have them in the same print statement.

Okay, so there you go. Let's remove those print statements.

Okay, so that is print. The next thing that I want to show you is how to get user input, but in order to illustrate that, we need to first learn about something really important, which are data types.

Now, when we write code in Python, we have different pieces of data that we can manipulate. There's actually a lot of different types of data, but I'm going to share with you the basic types, and you should memorize these.

Don't worry; they're not very complicated. Now, the first type is what's known as a string, or in Python, it's denoted with str. A string is literally anything that's surrounded in double or single quotation marks.

So even the number four, because it's in single quotation marks, this is a string. Inside of a string, you can literally put anything that you want. Okay, and that's just a string.

So if it has single or double quotes around it, it is a string data type.

Okay, now quickly before we go forward, I'm just going to show you something called a comment because I'm going to use this. A comment is anything that starts with the pound sign in Python.

Now, the pound just denotes a single line comment, and all this does is tell our editor to simply ignore whatever comes after this pound. So if I put pound string like this, it just says, "Hey, whatever you've written here, just ignore it. Don't worry about it; don't deal with it."

So if you ever see me write this, it's known as a comment.

Okay, so a comment, again, is just kind of to help illustrate or explain your code, and it's completely ignored when you actually execute the code.

At this point, you should understand that when we're executing code, we're doing it line by line, right? So we start at line one, go to line two, go to line three. So we start at the top and go to the bottom.

Okay, so strings—double or single quotes. Next, we're going to look at the int type. Now, an int is simply a whole number.

So a number like -9, a number like 0, a number like 23, a number like 45—these are all ints. Even large numbers like 3, 4, 5, 6, 7, 8—whatever—again, all ints.

These are known as integers. Okay, so whether it's positive or negative, if it's a whole number, so it doesn't have any decimal places, then we refer to this as an int.

Now, the next type that we have is a float. A float is simply a floating-point number. Now, that means any number that has some decimal place on it.

So something like 23.2, 23.0—even though this is technically a whole number since we have the 0, this makes it a float.

Okay, if we have 98.8, this is a float. If we have 0.123, again, a float. Float stands for floating-point precision. So if you ever have a decimal place, then the number is considered the type float.

Okay, lastly, we're going to look at the Boolean. Now, the Boolean is simply true or false. That's it.

Okay, capital True and capital False. Notice that if I just type a normal true or I type a normal false, it doesn't highlight in that fancy color because that's not a Boolean.

Now, Booleans are just meant to be used to kind of determine if things are, well, true or false. And again, they're simply these keywords: True with a capital T and False with a capital F.

So we have strings, we have ints, we have floats, and we have Booleans. There are a few other data types, but for now, that's it.

So let's stop this and let's move into the next thing I want to show you, which is the input statement.

Now, the input statement is something that allows us to ask the user for input. So in order to do that, we can use this function called input.

Now, the input function—and a function, by the way, is something you're calling using these parentheses. The function name is what's on the left-hand side, which is this, and what goes inside of these parentheses is kind of the arguments to the function, the things you're passing to it so it knows what to do.

Anyways, this is the input function. When I call this function, it allows the user to actually type something in the console.

In this set of parentheses here, one of the arguments to the input function is that we can pass a string, which is known as the prompt. So this is pretty much what you want the user to type in.

So I could say something like, "Hey, type your name," like that.

Okay, now if I do this, what's going to happen is Python's going to print out this line, and then right at the very end where my cursor is, it's going to allow the user to start typing.

So watch this. If I run the code, you can see it says, "Hey, type your name," and notice that I can actually start typing now in the terminal.

However, it's kind of a bit squished, right? It's actually right on the name, so I'll show you how to fix that. But the point is, as soon as you press enter, then it will stop, and it will actually collect whatever the user typed.

Okay, so let's go back here and let's fix that kind of squishiness by adding a colon and then adding a space. You always want to add a space at the end of your input just so that there's a bit of room for the user to type.

So now if I go here, I can go "Hello, World," and boom, it gets typed in, and that works fine.

I know it's a little bit difficult to read down here, by the way, just bear with me. I'll try to add some arrows and stuff on the screen.

Okay, so this is the input. Now, you might be saying, "Okay, well, that's great. You know, the user can type something in, but don't we want to know what the user typed in? Don't we want to maybe store that somewhere or use it later on?"

And yes, that's exactly what we want to do. So this is where we're going to talk about variables.

Now, you may have seen before in your math class, you might have had a variable like, you know, x equals 2 and y equals 3, and the teacher might have asked, "What is x + y to the exponent 2?" or something like that.

Now, in Python, you can use variables as well, just like in any programming language, and there are a few rules for when you use them.

Now, when you create a variable, what you need to think of is the following: you essentially have some kind of name, and you're storing some kind of value in that name.

So in this case, we have some bucket or some name called x, and that stores the value of 2. Now, the type of this variable is simply whatever the type of the value is it stores.

So what is the type of x? Well, this is an int. The reason for that is this type is an int.

Now, if we stored this, now the type of x is a string because we're storing a string in there. If it's true, now the type of x is a Boolean or a bool. If it's 21.3, now it's a float.

Okay, so that's a variable, and we can use variables in our program. We can access them, we can change them, and I'll show you some properties of that in a minute.

Alright, so that is x. However, when you make a variable, there are a few rules that you need to know.

First of all, variable names must be either uppercase or lowercase letters, and you can't use any special characters other than the underscore or numbers.

I know it's a little bit confusing, but my point is you can't make a variable like this: hello world equals 2. The reason for that is there's a space, and the space is a special character.

If you wanted to have a space in your variable name, if you wanted the variable to be multiple words, for example, then you would have to use the underscore.

Okay, so if we go hello_world, this is valid because we've kind of connected these words together.

In Python, if you want to have spaces, the convention is, again, just to use the underscore when you're making variable names.

For other stuff, like this, it doesn't matter, but for variable names, you cannot have a space.

Now, another thing that's important is I can't have special characters like the percent sign or the ampersand or anything like that. I am allowed to have numbers; however, I can't start my variable name with a number.

So if I try to do this, notice it's telling me this is invalid. So what I can do, though, is I can have a number maybe at the end or I can have it kind of in the middle.

I don't know why you'd want to do that, but you can if you want to. You can have numbers, but they just can only be after the first character.

Okay, another thing that's important is that you can have uppercase letters, and uppercase letters are different than lowercase letters.

So the variable hello_world with a capital D is different than the variable hello_world with a lowercase d.

Okay, so things that are uppercase are different than things that are lowercase. A lot of people mix that up, especially when they're just starting out.

So make sure you really check for the capitalization of letters because that will matter; that could maybe mess you up.

Okay, so the reason I'm showing this to you is because we want to be able to store what the user's name is, and that's actually how we're going to start our game or our project.

So to do that, we're going to make a variable, and we're going to say that the name, because that's kind of what the variable is going to store, is equal to the input.

Now, you can ignore the string thing; I'm actually going to disable that in one second, so don't worry about this grayed-out thing.

But what I'm saying is, "Okay, I want to assign whatever the user types in to the variable name."

Okay, and then if we want to see what the name is, we can actually print it out. Now, notice this time I didn't put single or double quotes. The reason for that is I'm accessing this variable.

So I'm saying, "Hey, I want to use this variable, which is of type string." I don't actually want to print out "name" because that would just print out "name."

We want to print out whatever the user's name actually was. So let me disable this, and then I'll show you how that works.

Okay, so that feature is now disabled, and if I save my code—and by the way, I probably should have mentioned this before, but every time you run your code, you do need to save it.

If you don't save your code, then when you try to execute it, it's not going to work. So make sure that it's saved, and you can kind of tell if you need to save it if you see this little white dot here.

You see the white dot up here? It means that the file is unsaved, and you can save that by going to File and then Save or simply hitting Ctrl + S.

Okay, so let's run this, and you see that we get, "Hey, type your name." I can put my cursor here, and then I can say "Tim," and whatever I type here, it's just going to print it out.

It's just going to kind of echo it back to me. That's because I'm printing whatever was stored in this variable.

So that's kind of the start of our program. Remember, we're going to be making a Choose Your Own Adventure game.

The Choose Your Own Adventure game works by allowing us to move down different paths. So what we can do is say something like, "Hey, you know, you're stuck in the middle of a maze. Do you decide to go left or do you decide to go right?"

If you decide to go left, then you get asked a question like, "Hey, you know, there is a bridge. Do you want to cross the bridge or do you want to go back the other way?"

If you decide to go right, maybe you get a different question like, "Hey, you see some water. Do you want to swim in the water or do you want to go back the other way?"

Right? These are just a few options you potentially could give, and the idea is there's all these kind of branching paths that you can go down.

It's a really great first project to work on, but the first thing we want to do is we want to ask the user for their name, and then we might actually want to ask them if they want to play the game.

So what I'm going to do is show you how we greet the user. To greet the user, I'm going to do kind of like a more advanced print statement here where I print out "Hello, comma, whatever their name is."

So this is whatever they typed in, and then I'm going to put a comma, and I'm going to put more text, and I'm going to say, "Would you like to play?" or I could say something like, "Welcome to my game."

Okay, that probably makes a little bit more sense. Notice I save the file, and then if I print this out, now we can type in our name, and you see it says, "Hello, Tim. Welcome to my game."

Okay, so that's how we embed variables in our print statement. We can do it like this.

Now, for some reason, if you didn't want to have the space here, I'll show you another way that we can do this with something known as string concatenation.

So I can add a plus sign here, and I can add a plus sign here, and what this does is kind of add these strings together.

Now, when you add strings together, all that's doing is just squishing them together. So if I add the string "s" and "t," then the equivalent is "st."

We just take them and combine them together. So if we add "hello" and we add "name," then we're going to get "hello" and then whatever the name is, and then this.

Okay, so I just want to show you this. This is called string concatenation—adding strings together. It's valid only if these are all strings.

So if we run this, you'll see that we get, if we enter our name, "Hello, Tim. Welcome to my game."

Now, you might be saying, "Well, I want the space." Yes, of course, we want the space. So to do that, we can just add spaces on either side, and then if we run this again, you see that we get "Hello, Tim. Welcome to my game."

Great!

Okay, now the reason this worked is because this is a string, this is a string, and this is a string. The reason we know "name" is a string is because whatever we get back from this input function is always going to be a string.

This is kind of an important part that you need to be aware of. Input always gives you a string, no matter what.

Okay, so even if I type in something like 2, which is technically an int, you see that this still works because even though we typed in 2, input just always converts this to a string.

So imagine this is always surrounded in double or single quotes. That's important for other things we might potentially do later on, and we'll kind of learn about how that works later.

Okay, so for now, we're greeting the user. Now, the next thing we want to do is we want to ask the user if they want to play.

So we're going to ask another question, and we're going to say, "Should we play?" is equal to, and I'm making another variable, and we're going to put an input function, and in the input function, we're going to say, "Do you want to play?"

Question mark, and then I'm going to put a space.

Okay, now notice that I named the variables something that makes sense for the value it's holding. So like "name" is storing the user's name; "should we play" is going to store whether or not we want the user to play the game, right?

So try to name your variable something logical. It's just a good habit to get into.

And notice I use the underscores here because we have multiple words for our variable name.

So we always want to test our code. So let's run this again. I'm going to say "Tim," and then it says, "Hello, Tim. Welcome to my game. Do you want to play?"

And then we can type something like "yes."

Okay, so things are looking pretty good. Now, the next thing we want to do is we want to see what the user typed in, and based on what they typed in, we want to start playing the game or not.

Right? If they type "no," we don't want to play the game. If they type "yes," well, we should play the game.

So how do we check that? Well, this is where we get into something known as conditionals or conditions.

Now, in Python, we have this Boolean type: true or false. We haven't really looked at it yet, but we're going to start using that now.

So what we want to do is we want to check if the user typed in "yes," and if they typed in "yes," we want to print out, "Okay, let's begin," and then start the game.

And if they typed in "no," we want to just leave the game.

So how do we check what they typed? Well, let's make a variable. We're going to call this "play" is equal to, and we're going to take "should we play," and we're going to check if this is equal to something.

Now, notice that we have this equal sign. This equal sign is what's known as the assignment operator; it assigns some value to some variable.

However, if we want to check if something is something else, we have to use these two equal signs.

Now, these two equal signs are the equivalence operator, so it checks, "Are these two things the same? Are they equal?"

So what we can do is we can check if "should we play" is equal to "yes."

So I'm saying, "Okay, we're using the double equal. We have a string 'yes.' We're going to see if that's the same as 'should we play.'"

And then we can print out the value of this "play" variable.

Now, whenever you use this equivalence operator, you're going to get a value of true or false back.

So if I run this and I type in "Tim," and then I type in "yes," you can see that we get true.

The reason we get that is this is what's referred to as a condition, by the way, evaluated to true.

Now let's type this again. Let's type in "Tim," and let's type in "no" or anything else, even "yes" with all capitals, and notice we get false.

The reason for that is that this was not true. This only checks if this string is exactly the same as this string.

So we need to make sure that we type things in with lower cases. We type exactly "yes," and then if that's the case, this will be true; otherwise, it will be false.

Now, quickly while we're looking at conditions, I'm going to show you a few other things we can do.

So with conditions, we have some other operators. For example, I could do something like 1 is less than 2.

Now, when we use this operator, this is the less than operator, and it will either return true or false.

Now, in this case, this condition is true because 1 is less than 2. If we switch it around and use the greater than operator, this is now false.

Okay, now we also have the less than or equal to, the greater than or equal to, and we have the not equal to.

Now, not equal to, this would actually evaluate to true because this does the opposite of the double equal sign.

This says, "Are these things not the same?" If they are not the same, it returns true; if they are the same, it returns false.

Now, an important thing to understand is that when you check the value of things here using these equal signs, they need to be the same type.

So if I tried to do something like 1 is equal to the string "1," this is going to evaluate to false.

The reason for that is that the string "1" is different than the int 1, just like the string "yes" is different than the string "yes."

Okay, so things need to be exactly the same, including their type, and later on, we'll look at doing some type conversions so you can see how we can check things that are different types.

Anyways, now we know if we should play or not. So how do we do something if we're playing?

Well, what we can actually do is we can use this fancy if statement.

Now, this if statement is really simple, and it works like this: we have if some condition is equal to true, so let's just write this: then do this.

Okay, this is the syntax of the if statement. If something is true, then do whatever is inside of this indented block. That's the simple version.

Now, let me write out how it actually looks. So in this case, we're going to say if play, because we know play is going to be a value that's either true or false because we're using this condition, we're going to put a colon.

It's very important that you have the colon, and then we have this indent. Now, it should automatically indent for you. You can either use the tab or you can do four spaces.

I recommend simply using the tab. So use the Tab key, say if play, we'll tab forward here four spaces, and then anything inside of this indent will run.

So if I go print, we are going to play, then we'll start playing.

Okay, and I can print multiple things inside of here, so I can print another print statement, and this will run.

So let's run this. Let's go Tim, do you want to play? I'm going to type yes, and then it says we are going to play.

Let's run this again. Okay, enter my name, Tim. Do you want to play? No, and then it just doesn't print anything because this was not true.

That's the if statement—pretty straightforward. You say if something is true, put the colon in, and then execute whatever is here.

So again, it's like this: if true, whatever's inside of this indented block will run.

And it has to be indented, by the way. Even if you have like one space off, it's going to give you an error, so make sure that it's indented properly, then it will run.

Now, by the way, what we can do is just write the condition directly here. So rather than having this variable play, we can say if "should we play" is equal to "yes," then we can do this.

So we can actually kind of combine conditions together. We can write multiple conditions; we can write the condition directly in this line.

There's a lot of fun stuff we can do. We're going to look at that later on, but the point is we can just write the condition directly here, and since whatever we're writing will evaluate to true or false, then it's going to first do this.

It's going to say, "Okay, is this true or false?" If it's true, then we run; if it's false, then we don't.

Perfect!

Now, you might be saying, "Okay, well, that's great, but how do I do something if we aren't playing? What if I want to maybe print out, 'Well, you know, we're not going to play?'"

Well, you could do something like this: you could say if "should we play" does not equal "yes," and then you could print something like, "We are not playing..."

So this is perfectly fine; this code will work. There's actually a better way to do this.

So all I've done here is I've written two if statements. First, we check this if statement. If this is true, we run this.

Then we come down and we check this if statement. If this is true, we run this, which really means "should we play" is not equal to "yes."

But there's a simpler way, and that way is to use something known as the else statement.

Now, the else statement is kind of like the other side of the if. So we have this if that says, "If this thing is true, do this," then we have the else, and else simply says, "If this thing wasn't true, then go here and do this."

So this means that only one of these two print statements will ever run. If it's true, this one runs; if it's false, this one runs.

So it's super useful when you don't want to write the condition again because we don't really need to; we just use the else.

So now if we run this and we go Tim, do you want to welcome to my game? Do you want to play? We type in yes, and it says we're going to play.

Now, if we run it again, Tim, do you want to play? No, then it says we are not playing.

Okay, so there you go. You just learned the if and the else statement.

Now, there's another thing that we can add here, and this is known as the else if statement or the elif statement.

Now, elif is kind of next in the chain. Now, elif is only allowed to be added after an if statement, and you can have as many elifs as you want.

We'll look at this in a second, but let me show you an example. So I'm going to say elif "should we play" does not equal "yes" in all capitals, then we'll say print in all capitals, "We are gonna play."

Just there's some distinguishing here.

So what I'm doing now is I'm saying, "Okay, we're going to add this elif statement." This elif statement only runs if this if statement is false.

So we say, "Okay, if 'should we play' is equal to true, go ahead and do this." If it's true, don't even bother looking at the rest of this; just move on with the rest of the code.

Now, if this is false, we actually move on to this elif statement, and now we check this condition.

So we say, "Should we play" is equal to true or is equal to "yes." If it is, we're going to print "We are going to play" in all capital letters.

So I check the lowercase "yes," and then I check the uppercase "yes." If this is true, we don't execute the else, which again can only come after the if or the elif.

Alright, now we have the option to add as many elifs as we want. So we could check multiple things like elif "should we play" is equal to "yes" with maybe a capital Y.

We could check that. We can check as many things as we want.

I'm going to show you a better way than doing this, but I just want to show you this elif statement because this is valid.

We can have multiple elifs, but the rule is you can only have elifs after an if statement.

So I can't just throw an elif down here because that doesn't make sense. It only makes sense in this chain of if, elif, and else.

Okay, you can have as many elifs as you want, but you can only have one else and one if.

I can add another if down here, but the point is this is kind of one chain of if statements, so just keep that in mind.

So let's leave this elif for now, and did we test this? Let's test this out and go Tim, and then yes, and it says we are going to play with all capitals.

Okay, perfect!

So now that we have that, I'm going to kind of show you a little bit of a better way.

So we probably don't want to be checking every single combination of upper cases for "yes," right? Or especially if it was a longer word.

So what we might want to do instead is we might just want to check, "Did they type yes at all?"

So whether it's like "yes" with capitals, "yes" with lowercase, "yes" with a capital E, it doesn't matter. We just want to check that.

Now, there's a way we can do that, and it's by using this lower function.

Now, when I add lower to the end of the input, what this does is simply convert this string to all lower cases.

So something like this: if we have a string like "Hello," and then we do .lower, it will convert this to be "hello" in all lower cases.

Now, just like lower, we have upper.

Okay, all it does is convert the string to be all uppercases.

So let's go back to the do lower.

Now, the point of using this is that we can convert the entire string to lowercase, and now when I check if it's equal to "yes," it doesn't matter what the user typed in as long as they typed "yes," whether it's all uppercase or kind of a mix or it's all lowercase.

Since we're comparing it as the lowercase version, this will be true.

So let me show you what I mean by that. Sorry, that was a little bit of a weird explanation.

I'm going to type Tim, and I'm going to type "yes" in all capitals, but this still runs because we converted the all-capital "yes" to all lower cases, and then we compared it to a lowercase string.

So make sure if you're converting it to lower cases, when you do the check, you check if it's all lower cases.

Okay, now you might be saying, "Okay, well, that's great, but what if the user types in 'y'?"

Well, clearly that's not going to work, right? Because "Y" is not equal to "S," whether it's uppercase or lowercase.

Well, this is where we get into something known as chained conditionals.

Now, this actually allows us to chain conditions together using one of three operators.

Now, we have the and operator, the or operator, and the not operator.

Now, the and operator checks if two things are true. So for example, you know, if 1 is less than 2 and 3 is less than 4, then this whole thing will be equal to true.

And this would be an uppercase.

So the way the and works is it looks at the left side and it looks at the right side, and if both these things are true, the whole expression evaluates to true.

That's it; that's how and works.

If one of these things is false, so this is false, for example, then this is going to be false as well.

Okay, whether both of them are false, as long as one of the things is false, the whole thing is false. If both of them are true, it's true.

Now, the other one we can look at is the or.

Now, the or operator is different. If either the left or the right-hand side is true, then it evaluates to true.

So in this case, this would be true because 3 is less than 4.

However, if both things are false, then it evaluates to false.

Okay, so that's the basics of and and or.

Now, with and and or, you can write like false or false like this, or you can actually put an expression.

So I can put like, you know, "should we play" is equal to "y" or, and then I could do "should we play" is equal to "yes."

So this is perfectly valid because we have a right-hand side, we have a left-hand side, and now the or just kind of combines the left and the right together using this operator and gives us a true or a false.

Now, this is exactly what we want to do, actually.

So I'm going to show you if I put this here in the if statement, what I'm checking now is, "Okay, if 'should we play' is equal to 'yes' or 'should we play' is equal to 'y,' then we're going to do this."

So what I'm doing is just checking two things. I'm checking if we typed in "y" or if we typed in "yes."

Now, I wouldn't want to put an and here because if I did this, this would never be true.

The reason for that is we can't have this variable be equal to "y" and be equal to "s" at the same time.

So hopefully that's making a bit of sense, but that's how the or operator works.

And now if we run this and we type Tim, and then we type "y," you can see that it says we are going to play.

Now, I know I'm going a bit slow, guys. I'm just trying to explain everything really from the very fundamentals.

Obviously, a lot of stuff to cover here. Don't worry; we're going to get into the project really shortly here and start building some more stuff.

I just want to make sure I don't skip over any fundamentals.

Okay, so we now understand conditions. We understand the and or operator, but we also have the not operator.

Now, the not operator isn't super useful to us right now, but what it does is just reverse a condition.

So if I had this whole thing, for example, and I actually wanted the reverse of it, so I wanted—how am I going to explain this?

I wanted if this thing is true to actually be false, and if this thing is false to actually be true, like I wanted to flip it around, I can use this fancy operator called not.

Now, with not, what we can do is we can maybe surround an entire expression in parentheses like I'm doing here, and this just flips the thing.

So it's easier if I show it like this: if we have not true, then this is equal to false.

If we have not false, then this is equal to true.

It just flips it.

Okay, so not false is true, and not true is false.

So if we have some large expression like not, and then we have this whole thing, which by the way, it's valid for us to use parentheses here, then if this thing evaluates to true, it just becomes false.

If the thing is false, it evaluates to true.

That's quickly how not works. I'm not going to show you more examples because we're not going to need it right now.

Okay, so now we have enough knowledge to actually start working on our game or our project.

Now, this game is actually super fun. I actually love building these, even though I'm a little bit more advanced than this now, but it's still a super fun thing to build.

And again, it's called a Choose Your Own Adventure game.

So what's going to happen now is that since we've asked the user if we want them to play, we now need to start presenting them with options for our game.

So typically, the first thing I like to ask the user to do is choose a direction.

So something like, "Do you want to go left or do you want to go right?"

Now, the game is a lot simpler when you just have two options, something like left or right.

However, you can have multiple options like left, right, straight, you know, backwards, whatever, and you can make that more advanced using the elif statement.

So the idea is we ask the user if they want to play. If they want to play, then only if they want to play, I want to ask them, "Do they want to go left or right?"

So I'm going to do "direction" is equal to, and I'm going to put another input, and I'm going to say, "Do you want to go left or right?"

Question mark. Notice I add my space.

Okay, so notice that I'm putting this inside of this if statement. So only if this is true do we execute this line; otherwise, we go into the else statement, and we just print, "We're not going to play."

Now this is where you'll start to see how the game kind of takes shape.

So now what I'm going to do is, since I asked them this question, I'm going to check if they want to go left or if they want to go right.

So I'm going to say if "direction" is equal to "left," and we can make this .lower by the way if we want to avoid any of those uppercase potential issues, then inside of here, I can print something like, "Okay, we went left."

And then I can do an else statement, and I can say print, "We went right."

So hopefully you get what's kind of happening here, but I'm putting an if statement inside of another if statement.

This is perfectly fine; this is okay. You can do this as many times as you want.

So I wrote my direction, "Do you want to go left or right?" I converted to lower case. I checked if the direction the user typed was left.

If it was, I said, "Okay, we went left." Otherwise, I said, "Okay, we went right."

Now this is where we would then add another question.

Okay, you went left. Now do you want to cross the bridge or do you want to swim in the water? Whatever you want to do, right?

You can be super creative here with this Choose Your Own Adventure game, and you'll see how that works as we continue building upon it.

Now, you might be saying to yourself, "Okay, well, I get it if they're going left, but the else statement triggers if they type anything else, right?"

If it's not left, so that means they maybe didn't type right, and maybe they just didn't type anything at all, or they typed some weird character that we don't understand.

So you're right. In this case, I'm just defaulting them, so they only have two options.

So if they type anything else, we're going to assume they're going right. But if you wanted to, you could say elif the direction is equal to right, then you do this.

But then if you do that, you'd want to have an else statement, and inside of here, you'd want to print, "Sorry, not a valid response or reply. You die."

Okay, just, you know, being nice and aggressive for them.

Anyways, I'm just showing you this is another option that you have.

So if you want to make sure they're typing in valid responses like left or right, then you can do that, and you can have an else statement where if they give you something invalid, you just, like, you know, don't continue the game.

There is a way to actually force them to answer this again, but we'll show that later because it's a little bit more advanced.

I also recommend that for your options, you put them in parentheses.

So do something like left slash right so the user knows what they're supposed to type because sometimes the options might not be clear.

So this just makes the game a little bit easier.

So I know I just coded a lot there without testing. Usually, not the best practice, but let's test this out and just make sure this works.

So you'll see that it starts to get pretty cool. I'm going to type my name, Tim.

Hello, Tim. Welcome to my game. Do you want to play? Yes, I would love to play.

Okay, we're going to play. Do you want to go left or right? Let's go left.

Okay, we went left. Nice!

Let's run it again. Tim, do you want to play? Yes. Let's go right.

We went right. And then I'm going to write it one last time. Tim, yes, and I'm going to type no, and then it says, "Sorry, not a valid reply. You die."

Okay, so we just exhausted all three of those options.

So now you can understand that this is kind of the structure of the game. We now have this direction, and now we know if we're going left or if we're going right.

So now we want to continue the game, and we want to ask them something if they're going left or right.

And you can continue asking as many times as you want, or you can end particular paths.

Like for example, if they type something invalid, we just end the game right there.

So if we go left, what I'm actually going to print is something like, "You went left and fell off a cliff. Game over. Try again."

Okay, harsh game, but that's what we'll do.

Now, if you went right, this is the correct direction, so we'll keep asking you more questions.

So we'll say something like, "Choice is equal to input," and then, "Okay, you now see a bridge. Do you want to swim under it or cross it?"

Okay, and then again for my options, I'm going to go swim or cross.

Okay, so they kind of know what they're supposed to type, and then we'll add a space.

And now we can go down here, and we can check their response.

So we can say if "choice" is equal to "swim," then, oops, sorry guys, ignore that.

Then we can say print, "You got eaten by an alligator. Not sure if that's how you spell alligator. You die. The end."

Okay, and then we can have another choice, right?

And we can say else print, "You found the gold."

Now, I know like this is a short game, and it doesn't really make a ton of sense what I'm typing.

I'm just trying to illustrate this to you because the point is you guys should code this out. I shouldn't code all of it for you, but you get the idea, right?

So like we had the direction. We said, "Do you go left or right?" If we went left, then game's over.

If we went right, then we asked them another question.

Okay, see a bridge. Do you want to swim it or cross it? And then we asked them, or we checked again.

So we said, "Okay, if you swim, then you lost, but if you decide to cross, then you won."

But we don't need to end it there. We can keep going and add as many if statements and else statements inside of this as we want.

So if we're saying that this is the correct choice, we can go again with another if statement, right?

And then we can have another if and another if and another if and another if, and we can keep going as much as we want and make this game really, really complicated and really cool.

And if you want to get really complex with it, I'm just making this a bit smaller so you guys can read all of it, you can actually let prior choices influence future choices.

So I'm going to give you a quick sample of what that looks like.

So when we ask them if they're going to play, we're going to ask them for a weapon.

So we're going to say "weapon" is equal to input, and we'll say, "Choose a weapon."

Okay, and then we'll give them the choice of a sword or an axe.

Okay, so now they're going to choose some weapon, and let's imagine, let's just assume that they're going to type in something valid.

Okay, and we can make this do lowercase as well.

Now later on, we could do something like if they chose to swim and the weapon was equal to an axe, then maybe they're okay.

Maybe they're able to kill the alligator because they chose the axe.

But if they chose to swim and the weapon was the sword, then maybe they're not able to get by because the sword's too heavy.

You get the idea, right? Like we can add other variables and make this way more interesting and make this really, really complicated.

That's why I love this as a first project because if you're really confused right now, you can just copy exactly what I have, and you can just kind of keep going through this system of adding ifs inside of other ifs.

But if you're really understanding this and you want to go a bit further, you can add extra variables, you can make it a bit more complicated, multiple options can be correct, and you can extend this game and make it really interesting.

So I'm going to stop the project here because this is just meant to be the first super beginner basic project.

I'm going to leave this code on GitHub; you can find the link down below if you just want to copy exactly this.

And now we're going to move on to the next project where we get a little bit more advanced and we build a calculator.

Alright, so now we're moving on to a text-based calculator. You'll notice I'm going to start going a little bit faster because we've already covered some of the basics, but obviously, we're slowly going to introduce some new topics.

You're going to continue to learn.

So for a text-based calculator, the idea is we want to allow the user to type in some numbers and kind of add them, subtract them, etc.

So there's a lot of different ways we could approach building this calculator, but in this case, what we want to do is we actually kind of want to parse an expression.

So for example, we want them to type something like 5 + 7, and then we want to get the result.

Now, we can make this even more complicated and allow them to type like multiple things and give an equal sign, and we can, you know, make this a lot more advanced if we want to.

Let's see, as we go through, I don't actually have this code already programmed out, so I'm going to try to build a simple version, and then we can kind of add on to it.

So keep in mind that I might potentially make some mistakes here. I want to show you what it's actually like to build something from scratch, so I'd prefer not to copy code I already have written.

Instead, kind of figure it out in my head so you guys get the full learning experience.

So when we build a project like this, the first thing I want to do is I want to ask the user for some input.

So let's do "equation" or let's do "expression," I guess, is equal to, and then we're going to grab some input, and we're going to say, "Type in expression."

Okay, and we're going to do a colon and then a space.

Now, we already know how that works. We're going to store the expression, and the expression is going to be entirely stored in a string.

Now, this is where I'm going to start introducing some new concepts to you.

The first concepts we're going to look at are arithmetic operators.

Now, in Python, we have a bunch of different ways to add, subtract, you know, do operations on numbers, but the important thing is that this only works on numbers—so floats or ints.

It doesn't work on strings; it doesn't work on Booleans. The type is really important.

But regardless, let me show you.

So let's say we have a variable like "result." We can do something like 1 plus 2, and Eugene has figured it out; it's equal to 3, right?

Like if we print out the result, this will be equal to 3. I don't think I need to show that to you, but we also have the ability to subtract.

So I can do 1 minus 2, and then the answer here would be negative one.

Okay, we also have the ability to do division.

Now, division is the forward slash, and the important thing to note here is that whenever you do division, the result of this is always a float.

Okay, so even if you do like 2 divided by 2, the result of this is a floating-point value.

You always get a float whenever you divide two numbers, and that's important because sometimes you can have mismatched types, and anyways, that's a thing.

Now, when you're doing addition, subtraction, these arithmetic operators, you're actually allowed to add floats and ints together.

So I can do something like 2.2 plus 3, and I will get 5.2.

This is an exception to some other operations that happen in Python, and I just want you to know that floats and ints, you can kind of combine them in these what are called arithmetic expressions where you're using an operator like the plus sign, the minus sign, the division sign.

Now, if you want to do multiplication, you can do that using the asterisk.

Okay, this is above the 8 on your keyboard. If you want to do an exponent, you can use two asterisks.

So we do 2.2, and this is like raised to the exponent 3.

Okay, that's what that means.

Now, if you want to do a modulus, this is the remainder after division. You can use the mod symbol.

The mod symbol gives you the remainder.

Now, the best way to show this is like this: if I do 4 mod 3, the way this works is we say, "Okay, 3 divides into 4 one time, so that gives us like 3, and then the remainder of that is 1."

Okay, so if we divide 4 by 3, we can divide 3 into 4 evenly one time, and then we're left with 1.

If we did 5 mod 3, then this is equal to 2.

If we did 6 mod 3, the remainder is zero.

Okay, this is actually very useful in a lot of different cases.

We can look at some examples later on.

Now, if you do something like 0 mod 3, then you're going to get zero because, well, 0 mod 3 is zero because you're dividing 0 by 3.

And also, another thing to note: if you try to do something divided by zero, like 3 divided by 0, you're actually going to get an error.

So if I run my code, you're going to see that it says, after I type something here, division by zero error.

So this is an exception. This can happen in Python if you do something wrong or you type in some wrong thing.

You'll get an error message that looks something like this.

This is perfectly fine; don't freak out. Just read what the error message is.

So in this case, it tells us zero division error: division by zero.

It actually tells us exactly where this is happening, so we know where to go and fix it.

Now, you're not going to see me run into that many errors in this video, but you very likely will run into errors.

So if you see them, again, please don't freak out. Just actually read what the error message is.

Most of the time, it makes sense. Some of the times it doesn't, but it should at least point you to where the mistake is in your program.

Alright, so we had result is equal to 3 over 0, and that didn't work because that is zero division.

Anyways, point is we have division, multiplication, addition, subtraction, modulus, exponent.

There are a few other ones, but these are the main ones you want to be aware of.

And when you do these operations, they happen in the order of operations that you're used to from your math class.

People make fun of me for this acronym, but this is the one that I remember: it is BEDMAS.

So this is brackets, exponents, division, multiplication, addition, and subtraction.

I know there's a million other acronyms out there for how that works; that's the one that I learned in school, so that's the one that I'll share with you.

If you want to know the order of operations for how you apply the arithmetic, this is what it is.

And yeah, you can put parentheses like you normally would in any mathematical expression.

The reason I'm explaining that is we're building a calculator, so you should probably know how those work.

Okay, so now that we know that, what I want to do is I want to kind of get a calculator working here.

So let's actually start with something a bit more basic, and I'm going to say "operand."

Operands are the things that are being operated upon by an operator, and we're going to start by saying "number1."

So we're just going to ask them for kind of two numbers.

So we're going to get "operand1" and then "operand2," and this is going to be "number2."

I'm then going to get the sign. I'm going to say "sign" is equal to input, and we'll say "sign," and we're going to ask them to type in like a plus, a minus, a division sign, whatever it ends up being.

So for now, let's just print out what this looks like.

So we're going to do "operand1," "sign," "operand2," just so we can see if this input collection is valid.

So let me run this. I'm going to say number1 is equal to 3, number2 is 9, sign is plus, and it says 3 + 9.

Okay, so that's kind of what we're looking for right now.

We built out the expression, but we want to obviously be able to add them.

Now, I want to show you what happens if I just try to add these things together.

So let's just go with something like result is equal to operand1 plus operand2.

Okay, and then we go print the result.

So let's just try this out just for kind of test purposes.

So let's go same thing: 9, 3, plus, and you can see we get 9 + 3.

And actually, look at what the result is: it's 93.

Now, the reason why we're getting that result is that the type of these two variables here are strings.

If you hover over them, actually, you'll probably see that it shows you a string there, not ints, which means when I try to add them together, what I'm actually doing is I'm adding two strings.

I'm performing a string concatenation, so I'm just tacking on that 3 to that 9.

That's not what we want. What we want instead is to add these as numbers.

So what we're actually going to do is we're going to convert these to ints.

Now, to convert them to int, you can use this fancy int function.

So I can simply surround a string with this int function, and it turns it into a number.

However, that only works if these are valid numbers.

But anyways, you'll see.

So let's run it, and you get the idea.

So number1 is 9, number2 is 3, sign is plus, and then you can see that we get 6.

Okay, so it worked this time because now we're adding these as integers or numbers.

But let me show you some potential errors we can run into.

So what if I type 9.2 and I type 3.5, and now I do sign as a plus?

Notice we actually get an error now

**Custom Error**

We're going to change this code quite a bit in a second, but I'm just trying to show you a basic calculator functionality.

Okay, so let's run this now and let's go for zero division. It says "division by zero; result is not defined."

Okay, let's fix that error.

So, we got to print the result, but we couldn't print the result because we didn't define a result variable. That's another thing that can happen if you try to access a variable that does not exist.

As you saw there, the reason we got that error is that this if statement was not true, so we went here, and we never defined the result variable. When I tried to print out the result variable, it didn't exist.

The way we can do that is by saying `result = 0` up here. That way, if we get down here and we didn't define the result, at least it has some value, and then we can print out what it is.

Okay, so we collected number one, collected number two, collected the sign. All of this is good, but what happens if we type in something like "hello" and "world" and I do something like a plus?

Notice we get another error. It says "could not convert string to float: hello." The reason for that is you can't add two numbers together that aren't numbers.

So, we actually need to do another thing here in terms of checking our operands to make sure these operands are numbers.

Let's go and add some more functionality here. If you want to check if something is a number, there are a few ways to do this, but one of the more effective ways is to use a try and except block.

Now, a try and except block essentially is going to try to do something, and if that thing fails, rather than throwing an error, it's going to allow us to handle that.

So, I'm going to do `try` and I'm going to say `operand = float(operand)`. Now, what this is saying is, "Okay, I want to try to convert this number to a float." That's what I'm trying, right? I'm just passing the float function here.

I'm going to try that in this block. Now, if that works, everything's good, but otherwise, I can handle the error in this except block.

So, inside of the except block, this is what happens if whatever was inside here failed. I can just print something like "invalid operand."

Okay, let's just comment this out for now.

Okay, I'm just going to delete it, and then I'll copy it back in. I just want to show you how this works.

So, if I go here and I type in, let's go "hello" again, and then we type in something like that, and we type in a plus, it says "invalid operand."

So, rather than giving us an error when we tried to convert this to float, it just went into this except block because an error occurred.

So, we're going to use this technique to make sure that our operands are correct. We're going to try to convert the operand to a float, and we're going to try to convert operand 2 to a float as well.

Otherwise, we're going to say "invalid operand" like that.

Okay, now we can print this in.

So, just the rest of the code that we had, and we actually no longer need to convert all of these individually to floats because they automatically will have been converted here if this worked.

Okay, so let's go and just remove all of these. Bear with me while I do that.

Okay, so I just removed all of the floats on here. The reason for that is now I'm converting it up here.

So, notice that's also a lot more efficient. I don't need to keep writing float a ton of times. I just try to convert it here. If it doesn't work, I'll print "invalid operand."

Now, there is an issue with this program that you might see, but let's run this, and I'll show you what's going to happen.

So, I'm going to do something like "hello" one, right? Invalid number two, and then a plus.

Notice it says "invalid ops," and then it prints "hello" one two.

Now, the reason this works is now we are simply adding strings. We're adding strings because these were not actually converted to floats, but it did pick up that these were invalid numbers.

So, really what I need to do is make sure that I only do all of this code here if the operands are valid.

So, there are a few ways that we can kind of write this. First of all, I can just take all of this code and put it inside of this try block.

Now, we need to make sure the indentation is correct, so I'm just going to tab all of those in.

Let's zoom out a bit so we can read this, and now this will only run if the operands are valid.

So, this is perfectly fine. We can do that, or I'll just show you another technique just because this is about learning.

So, what I can do now is I can paste my code down here, can get rid of all of this, and we can do something like this where I make a variable, and I'm going to say `valid = False`.

Then all I'm going to do is say `valid = True`, and then I can say if valid, and I can take all of this and do it inside of this if statement.

Now, what I'm doing is I'm setting what's known as a flag. So, I'm saying `valid = False`. I set it equal to false by default.

Now, I only set it to true if we reach this line. The reason for that is if any error occurs in this try block, as soon as it occurs, we immediately go to the except.

So, if either of these lines don't work, we never hit this; we just jump right into the exception.

So, now valid only becomes true if both our operands were successfully converted to numbers.

So, now if we run our code, we can test this out, and I can do something like, you know, "hello" and "123" and do a plus, and it says "invalid operand."

Whereas now if I do like 2.4 and 5 and I do a plus, it adds them together, and we get 7.4.

Okay, so that's kind of a good start to our calculator.

Now, the thing is we probably just want to keep asking the user for a number before we allow them to continue in the program.

Rather than us saying, "Hey, you have an invalid operand," just not letting the calculator work, we just want to keep asking them for a number until they give us something that's valid.

So, let's remove all of this and let's change things up a little bit so that I can show you how we use something known as a loop.

So, I'm going to scroll down, and I'm going to teach you about a while loop.

Now, a while loop is a loop that will continue to run while a condition is true.

So, I can do something like while I have some condition, and then this happens.

So, while this condition is true, whatever's inside of the loop is going to happen.

Now, the most basic example of this is something like the following: we have `i = 0`. I say while `i < 10`, and then I say something like `print(i)`.

Then I go `i = i + 1`. This simply adds one to the value of `i`.

So, if it's zero, then it becomes one. Then in the next loop, it becomes two, then three, then four, then five, then six, then seven, then eight, then nine.

As soon as it's 10, this is false, and we stop running this.

Okay, this is known as a while loop.

Now, I'm going to demonstrate this to you, so I'm just going to comment all of the rest of this out.

So, let's go like that.

Okay, and let me just quickly demo this to you. I'm going to explain it more in depth, but hopefully, you get the idea, and you see it prints 0 through 9.

Okay, so this is the most basic simple example of a while loop.

Now, another thing we can do is rather than doing this `i = i + 1`, we can actually go `i += 1`.

Now, this `+=` just adds whatever you have on the right-hand side to the value of a variable.

So, we can change the value of this variable, obviously, and if I do something like `i += 2`, well now I can add two to it.

So, if I do this and I run the code, now you see we get 0, 2, 4, 6, 8.

Okay, the reason why we don't print 10 is because this says `i < 10`.

So, when `i` is equal to 10, we don't print it because this is false.

But if we did `i <= 10` and then we ran this, you'll see that we get 10 because now 10 is valid for the loop to continue running.

So, the idea is simple. We have some condition. This condition can literally be anything that we want. If it's true, we keep running the loop. If it's false, we stop running the loop.

Okay, now it's very important that you don't create something known as an infinite loop.

Now, an infinite loop is a loop that never ends. If I did `while True` here, then this loop will just run forever because there's no way to end it because this condition is always true.

Now, there are, however, a few keywords you can use in the loop to kind of define its behavior.

So, for example, you have this keyword `break`. Now, whenever this break keyword is encountered, it immediately exits the loop.

So, if we do this, we have `break` here, and I run this, you'll see it just runs one time.

The reason for that is we run `print(i)`, we add two to `i`, and then we break.

So, as soon as we see `break`, we just exit the loop immediately.

The next keyword we have is `continue`.

Now, the continue keyword just goes to the top of the loop.

So, if I do something like `i`, I could do maybe like `print("run here")` just as a sample, you'll see that this print will actually never run.

The reason for that is I have this continue.

Now, the continue, when we see it, it doesn't end the loop; it just goes right back up to the very beginning.

So, if I run this here, you'll see this actually is an infinite loop. To end this, I have to do Ctrl + C.

Okay, let me get out of that. Sorry, and that's because we kept continuing.

So, rather than printing "run," we would print `i`, `i += 2`, `continue`. That means go back to the top of the loop and keep executing.

Okay, so `continue` and `break` are two keywords you should know about.

Now that we know about the while loop, I know that was a quick explanation, but that's kind of the point of this video.

I am going to show you how we use it to make sure that we collect valid input, which is a very common thing that you want to do.

So, we have this operand, and we're grabbing some input.

So, what I'm going to do is I'm just going to give ourselves some space, and I'm going to say `while True`.

And I'm going to do this.

So, while True, I'm going to collect this operand.

Then what I'm going to do here is I'm going to try to convert this to a number.

So, I'm going to say `try: operand = float(operand)`.

Now, if this works, I'm going to break.

Otherwise, I'm going to say `print("invalid number, try again")`.

Okay, so what I've just done is I've created a loop that continuously runs until the user gives me a valid number.

My condition is `True`.

Now, technically, this looks like an infinite loop. However, I have a place where I can break out of the loop.

So, I say `while True`, I say `operand = number one` and then ask them to input it.

I attempt to convert this to a float. If that works, then I just break out of the loop immediately.

Otherwise, I say "invalid number," and I ask them again.

Now, we're going to do the same thing for operand 2, but let's quickly test this.

So, if I go "hello," it says "invalid number, try again."

If I go "99.3D," it says "invalid number, try again."

If I go "99," then it goes to number two because this is valid.

Let's do something like that and a plus.

We got an error; that's fine because I haven't implemented this yet.

Anyways, that's the point.

Okay, so this is working.

So, what I'm going to do is I'm going to copy this, and we're going to do the same thing for operand 2.

So, we're just going to change this to `operand 2`.

And then we're going to remove this.

Okay, so now we have two while loops: one for grabbing the first operand and one for grabbing the second.

Now, we know by the time we get down here that we have two valid numbers.

Now, we don't know about the sign yet, and that's okay because the sign is just being used in these if statements.

So, if we wanted to, we could add another else here, and we could say "invalid sign" if they gave us something that's not one of the choices up here.

So, this is a good start to our calculator.

We're going to keep working on it, but let's write this out.

So, let's go maybe 34.5, let's go g, let's go negative h, let's go 34.6.

Okay, that works.

Let's go division, and you can see we get this number right here.

Okay, great.

So, that's working.

That's our calculator.

Obviously, there are more cases we can test, but you can see that it is indeed working, and we're getting valid operands.

Now, you might notice that this code here and this code here are the exact same.

Why is that?

Why do we have the exact same code twice?

Well, it's because we're pretty much doing the same thing; we're just storing this in a different variable.

So, now what I'm going to share with you is a new thing that we can look at called a function.

Now, a function is really just a reusable block of code.

We're going to make a very, very simple function here, and later on, we'll make some more advanced ones.

Now, to make a function, you use the keyword `def`. This stands for define, and then you give the function a name.

Now, the naming conventions are the same as a variable, so you can't use any special characters, you can't start it with a number, you can't have spaces, etc.

So, what I'm going to do here is say `get_number`.

Now, what I'm going to do is put a call in, and notice that I put this set of parentheses.

Now, that's because functions can accept arguments.

Notice that this looks awfully similar to something like the input function or the float function, and that's because it works the same way.

These are functions that are built into Python.

We're going to write our own function that has a function and can do something.

So, we're going to say `def get_number()`, and then we have a colon.

Inside of the indented block here, notice in Python, the indents are really important.

We can write something that will run when we call this function.

So, what I'm going to do is just take all this code and paste it inside of here, and then I'm going to fix my indentation.

So, notice I have a while loop, and then this needs to be indented two levels.

This needs to be indented three levels.

So, I fixed that by highlighting it and pressing tab.

Now, here's the thing: in this function, we can do whatever we want, and then we can actually return a value.

So, the return keyword allows us to give something out of the function.

So, we call the function; we don't need to return something, but if we want to, we can return a value, and this value will kind of go back to the user or whoever called this.

Now, in this case, what do we want to get from this function?

Well, we want to get a number, and that number is stored in the operand variable.

So, I'm just going to return operand.

Understand this looks a little bit confusing because you've never seen functions before, but the point is you have some function.

This function is callable. Callable means you can run it as many times as you want by just simply writing the name of it.

So, doing something like `x = get_number()` then this is going to ask the user for a number, and as soon as we get one, it's going to get stored in `x` because we're going to return it from the operand.

Okay, hopefully, that's making a tiny bit of sense.

Notice this works kind of just like the input function, right?

When we use the input function, we get some value, and then we get stored in this variable.

That's because the function returns that value to us as soon as the user types it in.

So, that's kind of the syntax, or that's how it works.

Now, because we have this return function now, or this return keyword, we don't actually need this break.

So, what I can do is I can replace this break, and I can even replace operand here with simply `return float(operand)`.

Now, the reason why this works is because as soon as the return keyword is encountered, the function ends, and when the function ends, this while loop will end as well.

So, I'm going to try to convert this to a float, and then I'm going to try to return it.

Now, if that works, then the function is just done.

We don't need to execute the while loop anymore.

If it doesn't work, then we print this out, and we keep going.

Okay, so that's my function.

Now, let's use the function.

So, I'm going to delete this other while loop, and I'm going to show you how we use this.

So, I'm going to say `operand1 = get_number()`.

I'm then going to say `operand2 = get_number()`.

Now, sorry, this is just operand, not operand one.

The reason why I wrote this function is because we did this two times.

So, rather than doing it two times, we just write it once, we turn it into this reusable piece of code, which is really what a function is, and then we can call the function doing something like this.

So, now our program is going to work exactly the same way.

And again, just to break this down, the function will return whatever the number is that the user typed once it's valid.

Now, since it's doing that, we can use this multiple times.

So, I can get the first number, and then I can get the second number by calling the function again.

Each time you call the function, it starts it over.

So, we don't continue from the last kind of iteration of this while loop; we just start right at the beginning.

So, we start the while loop again, and we do the entire process.

Now, let's run it just to make sure this works.

So, I'm going to say number one is equal to 45, and now notice it actually says number one again.

That's a little bit weird, but that's just because we have number one here.

So, I'm going to go, you know, 23, then ask for a sign, and you see that now this works, and we get 68.

Okay, so that's fine, but we probably don't want to put number one here, right?

We want to put number one, number two, maybe number three, four, five, however many numbers we want.

So, how would we do that?

Well, I can actually take in my function something known as a parameter.

So, in this case, I'm going to take in a parameter called number.

Now, when I write this, this actually means that I can pass a value to my function, and that function can use that value.

So, to do that, I would go here to where the function is being called, and I would pass the number one, and I would pass the number two.

So, now I'm specifying, "Hey, this is the first time I'm calling this function; this is the second time I'm calling this function."

And now number is going to be equal to whatever I passed as what's known as an argument here to the function.

Similar to when we use the print function, we pass a string.

To the input function, we pass another string.

These are the arguments to the function.

That's exactly what we're doing here.

And up here, you call this a parameter.

When you call the function, you call an argument.

Okay, that's just the names for it.

So, now what I can do is use this number.

Now, if I want to use this in the input statement, I can't use like what I did in the print statement and use a comma.

Using a comma doesn't work.

Instead, what I can do is I can do a string concatenation with a plus sign, or I can use another fancy thing, which I'll show you in a second.

But for now, we're going to say `number + " " + ":"`.

And then I want to show the number of the question that I'm asking for.

But if I just try to do number, what's going to happen now is I'm going to be adding an int to a string.

And if we try to do that, and we can try it, so let's run this, you're going to see that we'll get an error.

And notice it says "can only concatenate string not int to string."

So, when you try to add different types like a string and an int, you get an error.

So, what we need to do if we want to show this is put `str(number)` around number.

So, what I'm doing now is I'm converting this number to its string representation, just like we converted a string to a number.

And now we're able to add them together using what's known as string concatenation, right?

Using this plus sign, and then afterwards, I'm just going to add a colon and a space just so it looks a little bit nicer.

So, now watch this.

It's going to say number one, 45, number two.

Let's try "hello."

Doesn't work.

Okay, let's try 67.8.

Okay, it works.

We're going to go with division, and you see we get this value.

Okay, so this is how you pass a parameter to a function.

And now notice we've created this reusable block of code.

We can call it multiple times.

We can pass to it some value that kind of changes its behavior and how it works, and it can return to us a value that we can then use later on in our program.

So, that's pretty much it, guys, for project number two.

Let's quickly run through the code one last time, and then we'll move on to something slightly more complicated.

So, we have this function again, a reusable block of code.

We could use it multiple times.

We take in a parameter.

This is something that changes, can be different, and can affect the behavior of the function.

This is called number, by the way.

If you want to accept multiple parameters, you can put a comma, and you can take multiple.

And then if you wanted to pass another one, you would just pass it like this.

Okay, so you can take as many as you want in this order.

A lot of other properties of functions, but that's the basics.

Then inside of here, we have a while loop.

This loop will continue to run while this is true, and the only way we get out of it is if we have a return statement or we have something like a break or this word to be equal to false.

We say `operand = input("number " + str(number) + ": ")`.

So, we're just converting the string or converting the number story to a string so we can add it to this string in the input.

The reason we can't use the comma is because the comma doesn't work inside of input; it only works inside of print.

Okay, when you want to print multiple things, then we try to convert this to a float.

If it works, we return it.

If it doesn't, we tell them it's an invalid number, and they need to try again.

Okay, here we simply call the function two times: one for the first operand and one for the second operand.

We then ask the user for the sign.

We don't do any validation on this because we have this kind of if statement chain that works here.

We say the result is equal to zero.

If the sign is a plus, we're going to add the two operands together.

If it's a minus, we're going to subtract them.

And if it's a division sign, we're going to do an additional check just to make sure we don't have a division by zero.

Now, if we have a division by zero, we're just going to print "division by zero."

Otherwise, if it's not zero, we are going to divide the two operands.

Lastly, we check if it's multiplication; we multiply them.

And then if the sign was not valid, we print that out, and then we finally print the result.

Okay, that's it for our basic calculator.

Again, this code will be available on GitHub.

Now, let's move on to the next project.

So, let's move on to the next project, which is going to be a contact management list app system, whatever you want to call it.

We're going to collect the user's name, age, their email, and then store that in a place where we can maybe search through it.

We can store it persistently, we can delete people, we can add people.

You'll see what level of functionality we want to add as we go through the project.

But first, before I can show that to you, I need to teach you about for loops and lists.

So, let's start with the for loop.

Now, a for loop is just like a while loop. In fact, anything you can do with a for loop, you can recreate with a while loop, except you use it when you know how many times you want to iterate.

Rate is like how many loops you want to do.

So, I'm going to show you a basic example of a for loop that can count to 10.

Okay, so I'm going to say `for i in range(10):` and then we're going to print `i`.

So, let's break this down.

When we make a for loop, we write `for`, we then have some kind of variable.

This variable keeps track of the current iteration.

Iteration is like what loop we're currently on.

Now, this will start at zero, and it will go all the way up to 10, but not include 10.

So, the syntax is `for i in range(10):`.

Range allows us to iterate by a certain value.

So, in this case, we just use 10.

It means we're going to start at zero, we're going to go up by one, and we're going to stop at 10.

But there are a lot of other arguments here as well, and I'll show you those in a second.

Then we put a colon, and we print `i`.

When I print `i`, I'm just printing what the value of this is, and you'll see when I run this, we just get zero through nine.

If we change this to five, we get zero through four.

Okay, so we go up to, but we do not include this last value.

Now, in this range function, we can pass multiple arguments.

So, for example, I can pass one comma 10.

When I do this, it indicates that I'm starting at one and I'm stopping at, but not including 10.

So, when you have two arguments, the first is a start, the second is a stop.

So, if I do this, you'll see that we go now from one to nine.

Now, if I wanted to go from one to ten, I would make this eleven, and you can see that I go from one to ten.

Now, the last argument that we can add is what's known as a step.

Now, a step allows us to determine the increment at which we go up.

So, by default, it's one, but if we make this two, we're going to go from one to three to five to seven to nine to eleven.

So, let's see this, and you see we go one, three, five, seven, nine.

Okay, now we can change the step; we can even make this negative.

So, for example, if I go maybe eleven and then one like this, and I make this negative one, we're going to start at eleven, go to one, subtract negative one, and you see that we now start at eleven and we go down.

Okay, so that's the for loop.

Range allows us to do this using numbers.

Okay, now there's also ways to iterate over objects or a list, and we're going to look at that in a second.

Okay, so that's the basics of the for loop.

Again, use the for loop when you know exactly how many times you want to iterate, whereas you use the while loop when you're not really sure.

So, in the previous example, we used a while loop because we didn't know how many times the user was going to give us an incorrect number.

Now, this, what I just showed you, like we could implement this with a while loop; it's fine.

We can build it with a while loop.

This is just a bit faster using the for loop.

Okay, next thing to look at here, and actually, let's go back to the for loop for one second, is there are two other keywords that we already learned about: break and continue.

They work the exact same way as before, just within the for loop.

Okay, so if you use continue or you use break, you can use those in here.

You could do like an if `i == 5: break`, whatever you want.

Okay, just want to make clear that you can use those inside of the for loop.

Now, the next thing to look at is lists.

Now, lists look like this, and it is a collection of elements in a particular order.

So, for example, we might have a list that has the elements 1, 2, 3, 4, 5.

We might have a list that has the elements "apple" like this, and maybe "pears."

Okay, so some fruits.

We might have a list that has true, true, false.

We might have a list that has other lists inside of it.

This is valid, although we're not going to look at that right now.

Okay, so the point is that lists can store different types.

They can store numbers, they can store strings, they can store really anything that you want, and they're stored in a particular order.

Now, lists are really useful when we want to know the order in which elements exist, and we want to potentially search for elements and kind of store them in a collection.

Right?

So, rather than us having five variables to store number 1, 2, 3, 4, 5, we can have one variable.

We can call this, you know, the numbers list, and we can store these multiple elements inside of there.

Now, let's look at a few properties of lists.

So, I have this numbers list: 1, 2, 3, 4, 5.

Now, every single position in this list has what's known as an index.

Now, the first index is zero, and then one, two, three, and four.

Okay, so we always start counting with zero when we're talking about counting in programming and with computers.

So, if I want to access this first number here, I would use what's known as index zero.

Now, you could call this element one because it's like in the first position, but the index, which is what we're referring to, is zero, whereas the last index is always whatever the length of the list is minus one.

So, in this case, we have five elements, so the length is five.

So, if we subtract one, that gives us four; that's the last element.

Now, the reason I'm sharing that with you is because if we want to access these elements, we do it like this.

I can say `print(numbers[0])`, and then I can use this index syntax.

So, when I use these square brackets here and I put some index, in this case, it's zero, that refers to this first position or this zeroth index.

So, if I print this out, it gives me whatever's in that position, which is one.

Okay, so I accessed this position.

Now, if I did maybe four, for example, you'll see that I get five because I'm accessing the last index in this list.

But if I try to do five, you'll actually see that I get an index error.

It says "list index out of range," and the reason for that is that I'm trying to access an index that doesn't exist because we only have up to four; we don't have up to five.

So, if I try to access five, it gives me an error.

So, you need to keep that in mind.

You can only access the indices that exist.

Now, a shortcut if you want to grab the last element is you can actually use negative indexing in Python.

Like, the last element is -1; the second last element is -2.

I don't need to go through all of that, and another way to do this is you could take the length of the list, which you can get by using this `len` function.

So, if you do like `len(numbers)`, it tells you five because it gives you the length of this list.

And I can subtract one.

So, if I did like `len(numbers) - 1` inside of this index here, then it's going to give me five because it's accessing the last element.

Okay, by the way, `len` also works on strings.

So, if you did like `len("string")`, this is going to give you six because this is six.

Perfect.

So, that's the basics on lists.

Now, with lists, we have a few methods you need to be aware of.

The first is `append`.

So, if I did `numbers.append(10)`, and then I print out the numbers list, what's going to happen now is 10 is going to get added to the end of the list.

So, you can see I now have 1, 2, 3, 4, 5, and then 10.

Okay, so just `append` is how you add something to a list.

This is useful because a lot of times lists will start empty, and you'll kind of make them grow by adding elements to them.

Now, another thing you can do is you can remove elements.

So, if you go `numbers.pop()`, what you can do is place the index that you want to remove inside of here, and it will remove the element at that index.

So, for example, if I want to remove three, I do `numbers.pop(2)`, and then now look, I only have 1, 2, 4, 5 because I removed whatever element was at index 2.

So, those are the main ones: `append` and `pop`, and then getting the length is with `len`.

There's a lot of other stuff you can do with lists, but I'm not going to go through all of it here.

I'm just showing you the basics.

Now, what we do want to look at, though, is how we iterate over a list.

So, a lot of times what you actually want to do is you want to look at all of the elements that exist inside of a list.

So, if we change this example to be, you know, `fruits = ["apples", "pears", "peaches"]`, what we want to do is we want to actually maybe print out all of the different elements in this list.

We want to look at them or do something with them.

So, to do that, we can use a for loop, and we can actually say `for fruit in fruits:` and then I can print my fruit.

Now, what this does is each iteration, it just gives me the next fruit in this variable.

So, if I print this out, you'll see that we get apples, pears, and peaches.

Okay, it just loops through the list.

Now, another way to loop through this list is to do the following: we can say `for i in range(len(fruits)):`.

Now, when I put the length of fruits, this means that `i` is going to go up to, but not include the last element here or the length of this list.

So, in this case, it's going to go up to three, but not include it.

So, `i` is going to be 0, 1, and then 2, and that allows me to do `fruit = fruits[i]`, and then I can print out the fruit.

So, what I'm doing is grabbing the fruit at the index.

So, this is called looping by index.

The other is called looping by item or by element.

Now, here we're just grabbing all of the indices that exist: index zero, index one, index two.

We're getting the fruit that exists at that index and then printing it out.

So, if I save this and I run, you can see that we get the same thing.

Now, you might be asking, "Why in the world would you want to do that?"

Well, sometimes you actually need access to the index as well as the element, or you just want to know what the index is.

In that case, you use this; in other cases, you just want to know what the element is.

You don't care where it actually is, and in that case, you can just use the other way I showed you where you say `for fruit in fruits:`.

Okay, that's the basics on lists.

There's a few other things that we can do, but now let's get into the project.

Okay, so what I want to do here is I want to kind of write some code that allows me to ask the user to enter a name, an age, and an email, and then I want to like store that person.

In this case, in a list, and then maybe print out all of the people that we have.

We're going to start with some basic functionality, and then we'll get more complicated.

So, to start, I'm going to make a function, and I'm going to—or actually, let's not make a function.

I'm coding this again from scratch, so bear with me.

Let's make some basic inputs.

So, let's say `name = input("Name: ")`.

Okay, and let's go `age = input("Age: ")`, and let's go `email = input("Email: ")`.

Okay, so I'm going to ask for the name, age, and email.

Once I have this, I'm going to say `names = []`, `ages = []`, and `emails = []`.

And I'm going to add all of these into the list.

So, let's actually put these above the inputs.

Okay, and we're going to say `names.append(name)`, `ages.append(age)`, and `emails.append(email)`.

Okay, and then I'm going to print out my names, my ages, and my emails.

I know I went a little bit faster.

Let me zoom out so you guys can see this.

We created three lists: an empty list called names, an empty list called ages, and an empty list called emails.

We collected a name, age, and email, and then what I'm doing is I'm just adding those into the corresponding list.

So now the index zero stores like this person, right?

Index zero in names is their name, index zero in ages is their age, index zero in emails is their email.

Okay, so let's run this.

So, let's enter a name.

Let's go Tim, age 24, email tim@gmail.com.

Don't email that; it's not my real email.

And you can see now that we get our three lists.

Okay, that's nice, but the whole point of using a list is to store multiple things.

So now I'm going to get multiple inputs.

So, I'm going to say `for i in range(3):` and I'm going to put all of this inside of here.

And then I'm just going to print up here, and I'm actually going to print `i + 1` and then go comma, and we'll say input.

Now, the reason for that is going to say like "input 1," "input 2," "input 3," just so we know what input we're currently on.

Okay, so let's do this, and let's go name, age, email.

Let's go Joe, 45, joe@hotmail.com.

Let's go, you know, Jane, 54, jane@mail.com.

And you can see now that we get three lists.

Okay, so we have Tim, Joe, Jane, 24, 45, 54, Tim, Joe, and Jane.

Okay, so I'm just trying to show you kind of how we use the list here to store three different elements.

So, that is working well.

However, you'll notice that it's a little bit weird, like how this is being stored.

We have a name, we have an age, we have an email, and if I want to access a person, I need to access them from three different places, right?

Like, I have to look in the names, I have to look in the ages, and I have to look in the emails.

Now, the reason I did this is just for demonstration purposes.

There's actually a better way to go about storing information that's kind of correlated with, in this case, a person.

Rather than having three lists, we can have one list, and we can call this maybe `people = []`.

Now, we're going to get rid of all of this, and I'm going to show you a new thing in Python called a dictionary.

Now, a dictionary looks like this, and it is a key-value pair.

It allows you to associate something like name with a name, and then to have another key like an age and associate that with an age, and then have something like an email and have that associated with an email.

So, the way a dictionary works is you can create one like this.

So, you could say maybe `d = {}`.

This is an empty dictionary.

What you're seeing right here, you can manually populate it with keys like I was showing you, or you can add keys like this.

You can say `d[key] = value`.

Now, the value needs to be obviously a real value.

I'm just kind of giving you an example of how this works.

Now, the key can be anything that you want.

It could be a one, it could be true, it can be a string, as you saw, and the value can be anything you want as well.

Now, in this case, it's really useful for us to use a dictionary to represent a person because we can have different keys with the properties of that person.

So, a key for their name, a key for their age, and a key for their email.

And then the way you access those keys is just like you access them in a list.

So, if I wanted to access a person's name, I would just say `d["name"]`, and then I can access it.

Now, if I wanted to assign it, I could say `d["name"] = "Tim"` and make this a string.

So, there are all different properties of dictionaries.

You're going to see how they work in a second, but I just want to show you that what we're going to do is make dictionaries, and then we're going to store those dictionaries in the list.

So, this is going to look a lot cleaner.

And again, just bear with me; it will make sense in a second.

So, now I'm going to say `person = {}`.

And inside the dictionary, I'm going to define my fields.

So, I'm going to say the name is whatever the name is they typed in.

I'm going to say the age is whatever the age is they typed in, and the email is whatever the email is they typed in.

So, now I've created this person, and then I'm going to say `people.append(person)`.

And then down here, I'm going to print my people.

Okay, so we're creating a dictionary.

A dictionary allows us to have keys associated with values, and then we can add that dictionary as an element in our list.

And now we're storing all of these people and kind of keeping them together.

Not sorting, storing.

So, let me run this, and we're going to say Tim, 24, tim@gmail.com.

I'm just going to make this fast.

Let's go Billy, 34, bill@hotmail.com.

And Jane, 35, jane@mail.com.

And you can see now that we get kind of these dictionaries, right?

Where we have the name, the age, and the email, the name, the age, and the email, and the name, the age, and the email.

Okay, so now we have dictionaries inside of a list.

So, this is looking a lot better.

Now, what I want to do is start actually adding some functionality to this system.

So, what I want to do is I want to have this list, but I want to start by beginning and asking the user for some function so they have the ability to potentially add someone.

But I also want to give them the ability to delete someone and to maybe search for someone.

So, let's start by—we'll keep our people list up here, and we're going to make a function actually called `add_person()`.

Okay, now I'm going to take all this code, so we're going to get rid of this for loop because we don't need the for loop.

And I'm going to leave this in here, and rather than appending this person, I'm just going to return person like this.

Okay, so I made a function called `add_person()`.

We collect the name, the age, the email, we create a person, and then we just return them.

Okay, simple function that will add a person for us.

Now, by the way, it's good practice to put your functions at the top of the code, and then anything else you put at the bottom.

Okay, we have the ability to add, but now we need like kind of some commands the user can type.

So, I'm going to do a print statement.

I'm going to say "Hi, and welcome to the contact management system."

Okay, and we're going to say in another print statement, we'll just do a print so there's like a line break, so there's a bit of spacing.

And we'll print, "You can add, delete, or search."

Okay, so let's add our single quote.

By the way, if you want to use quotes, you can embed like the opposite type of quote inside of the other quote.

So, in this case, I'm adding single quotes inside of the double quote.

But if I try to add another double quote here, you'll see that it kind of cuts it off and gives us an error.

You can actually escape this using a backslash character, and then it will like ignore that as a Python quote instead of being inside of the string quote.

But I don't want to confuse you, so we're just using single quotes here.

Okay, so we're going to say print, "You can add, delete, or search," and actually we're going to make this an input.

And we're going to say that the command that the user typed is equal to this input.

Then we're going to check what command they type.

So, we're going to say if command is equal to "add," then we're going to call the `add_person()` function.

Okay, we'll say `elif command == "delete":` then we'll do something here.

I'm going to write `pass`.

Pass is a keyword you can use when you don't yet know what you want to put, but you need to have something indented because that's how the elif works.

We'll say `elif command == "search":` then we will pass.

And then otherwise, we'll just print "invalid command."

I know I'm going fast, guys.

Feel free to pause the video.

I just want to not super slow because we've already learned a lot of this stuff.

Okay, so we have the command input.

You can add, delete, or search.

Let's convert this to lowercase, and now this ideally should work when we try to add a person.

However, the `add_person()` function simply returns a person.

So, what we're going to do is say `person = add_person()`, and then we're going to say `people.append(person)`.

And we need to make our people list because I guess I deleted it.

So, we're going to say `people = []`, so that's going to store all of our people.

So, if the command is "add," we're going to call `add_person()`.

It's going to give us a new person.

We're then going to add that by appending that into our list.

So, let's just try this.

Okay, let me get rid of that.

Now, actually, we should probably print out something like "print(person added)," and then we can just print our list down here so that we can see if this is actually working.

So, don't want to print person; I want to print people.

Sorry, guys, I've been doing this for a while now; my brain is getting foggy.

All right, let's run this.

Hi, welcome to the contact management system.

You can add, delete, or search.

So, let's go add.

It says name Tim, age 24, email tim@gmail.com.

It says person added, and then it gives us that person in the list.

Perfect.

Now, the thing is we probably want to keep running this until the user tells us to quit.

So, let's actually add a while loop, and let's go `while True:` and let's put all of this inside of here.

So now it'll just keep running all of this until eventually we type "quit."

What we can do is we can add maybe another else if here.

We can say `elif command == "q":` which will be quit, then we can break, and that will just break out of our system.

Okay, so now let's just add "add" and "q" for quit.

Okay, so now we have some commands.

We have kind of the ability to trigger these different commands, and we have the ability to add a person.

We probably want to now maybe be able to delete people, and we also probably want to list our different contacts or potentially search for them.

So, in the delete command, I think what we're going to do is we're just going to list out all of the different contacts that we have with a number beside them.

So, like number one, number two, number three, number four.

We'll ask the user to type in a number, and then based on the number they typed, we'll delete that corresponding contact.

So, let's make a function called `delete_contact()`.

Okay, now inside of here, we're going to ask the user, or we're going to print out all the contacts we have, and then ask the user to type them in.

So, for `delete_contact()`, we're going to take the people list.

Okay, so we have all the people, so we're going to take that in here, and we're going to start printing them out.

So, we're going to say `for i, person in enumerate(people):`.

Now, what `enumerate` does is it gives you the index as well as the element for every single element inside of your list.

So, in this case, it's going to give us each person in our list as well as what position they're in.

So, we can print out the number and then the person.

So, what I'm going to do now is the following.

I'm going to say `print(i + 1, "-", person["name"], "|", person["age"], "|", person["email"])`.

So, this is how I would access their name, right?

Because we have name, age, email.

So, I want to access the name.

I'll do it like this, and then I'm going to do a pipe, and then I'm going to do person["age"], and then I'm going to do a pipe, and then person["email"].

Okay, so what this is doing is just kind of like printing out one of the people.

So, before we go any further, let's just call this function and test this.

I'm going to go `delete_contact()` and I'm going to run this code.

And I'm going to go, first of all, I need to add them.

So, we'll see ones.

I'm going to go add Tim, 24, tim@gmail.com.

Person added.

You can add, delete, or search.

Okay, let's go with delete, and we got an issue.

It says "missing one required positional argument: people."

Okay, so for `delete_contact()`, since we're going to take in the people list, we need to pass the people list.

So, let's pass it like that and let's run this code.

Let's go add name Tim, age 24, email tim@gmail.com.

Person added.

Let's go delete, and when we go delete, it for some reason did not print out our person.

Okay, so another error here.

I'm going to go Q to quit.

Good job that came up, actually, so I can explain this to you.

The reason we got that problem is that inside of our while loop, we define our people list.

So, every time we enter the while loop, we're recreating this list, which is deleting all of the people that we had previously because we're setting it equal to an empty list at the start of the loop.

So, what I need to do is take this and put it outside of the while loop, so like here.

So now this will be persistent throughout each loop.

Okay, sorry about that, guys.

But let's run this again, and let's go with ADD Tim, 24, tim@gmail.com.

Okay, person added, and let's go delete.

And you can see it says "1 Tim 24@gmail.com."

Great, so we're going to print out all of those, and then what we're going to do is ask the user to enter a number.

And assuming the number is valid, we're going to delete that person.

Okay, so we're going to go here, and we're going to say input "Enter a number to delete: ".

Okay, and we're going to say `number = input()`.

Now, firstly, we need to convert this to a number, so we're going to say `try: number = int(number)`.

And then we're going to say except, and we're going to say print "invalid number."

Now, if this number is valid here, we also need to make sure that it's within one and whatever the number of people is that we have.

So they don't give us a number like -2 or something that's way more people than we have access to.

So we're going to try this; however, we're going to have to put a while loop here.

So we're going to say `while True:` because we're going to have to keep asking them for this.

So while True, `number = input("Enter a number to delete: ")`.

We're going to try to convert this to an int, and then we're going to make sure that this number is within the range.

So we're going to say if the number is less than or equal to zero or the number is greater than the length of our people, then we are going to print "invalid number, out of range."

Okay, so they kind of know what the issue is.

Otherwise, we are going to break.

Okay, so what we're doing is the same thing as before.

We're going to keep asking them for a number to delete.

When they give us a number, we're going to try to convert that to an int, and we're going to say if the number is less than zero or the number is greater than the length of people, then it's invalid.

So we're going to tell them it's invalid, and then we'll go and ask them for a number again.

If this didn't work, so they didn't type a number, we're going to tell them "invalid number."

If this did work, we're going to break out, and now we'll have access to a number which we can use to delete the person.

So, what we're going to do now is we are going to say `people.pop(number - 1)`.

The reason we're going to subtract one is because when we print out the numbers, we're printing whatever the index is plus one.

So if they do number one, really we want to delete index zero.

So we're subtracting one when we do pop.

That's going to remove that element from our list.

Now, we can get into a feature here known as mutable or immutable.

I don't really want to dive into it because it's a little bit of a long explanation, but one thing you need to know is that when you pass a list to something like a function, you can actually modify that list within the function, and any changes you make will persist outside of it.

So, in this case, I pass this people list to this function, and when I delete something from the people list here, it doesn't just delete it from like this version of the list; it deletes it outside as well.

So, this is what's known as a mutable property.

Like, mutable means can be changed.

So, we are actually modifying the list within this function when we delete something.

We don't have to do that; there are other ways to do this, but I just quickly wanted to kind of cover that.

I know this is getting a little bit advanced, but this is, you know, the last project, so I'm trying to teach you a lot of stuff.

Okay, so `people.pop(number - 1)`; that should delete the contact for us.

So, let's try this out now.

I'm going to run this.

I'm going to say you can add, delete, or search, and Q for quit.

So, we're going to go add.

I'm going to go Tim, age 24, email tim@gmail.com.

And let's add one more person, so add Joe, 45, joe@hotmail.com.

And you can see that it says person added.

Now, let's try to delete.

When we delete, we get one and two, and it says enter a number.

We enter zero: invalid.

If we enter four: invalid.

If we enter hello: invalid.

If we enter two, then we delete Joe.

Now, really, it should say "person deleted," but it did actually delete them.

And if we go back to delete, you can see that we only now have Tim.

So, let's actually get out of this.

I'm just going to hit Ctrl + C.

If you need to exit your program, by the way, you can hit Ctrl + C, and that will allow you to get out.

And now we're just going to print "person deleted."

Okay, great.

So, that's how you delete and how you add.

And we probably want to do something like where at the top of our while loop here, we print the number of contacts that we have.

So, we're going to say "contact list size: " + str(len(people)).

Okay, so this program is already getting a little bit long.

Let me zoom out a bit so we can read a little bit more.

But we have the ability to add a person, to delete a person, which turned out to be a little bit more complicated than I thought.

And then we are now going to add the functionality to search.

So, if we want to search for someone, we probably want to search based on their name.

So, we're going to make another function.

We're going to go `def search()`.

In order to search, we are going to need to take in the people list again.

And what this is going to do is it's going to look through all of the people and try to find names that match with the name that the person typed in.

So, we can zoom in a little bit.

And the first thing we're going to do is say maybe `name = input("Search for a name: ")`.

Okay, we're going to ask the user to type in a name.

Now, by no means is this going to be the best search algorithm ever implemented, but I'm going to show you a basic one.

So, we can do some more advanced implementations with the list here.

And what I'm going to do is make a list called `results`.

Now, this is going to store all of the people that kind of match a particular result, just so that it allows us to kind of quickly search through the different contacts.

So, I'm going to say `results = []`.

What I want to do now is I want to loop through my list, and I want to try to match this name or at least part of this name to part of the names of the people that we have.

So, pretty much if you were to type like "TI," I would want all of the Tims or all of the, you know, Timothy or anyone who starts with "TI" would want that to pop up.

So, that's kind of how we're going to do the search here.

So, to do that, we need to loop through all of our names.

So, we're going to say `for person in people:` and then we're going to go `name = person["name"]`.

And what I'm going to do is check if the string that this user typed is the same as the start of this person's name.

So, I'm going to say actually if `search_name in name:`.

Now, this isn't quite a perfect search, but what it's going to do is check if the string of whatever the search name is exists inside of this person's name.

So, let me give you an example.

So, if I have like Tim, and then my last name is Rea, as many of you guys know, and the search string is simply Tim, well, this string Tim exists inside of this name.

So, this would return true.

But also, like if I did "I," well, the "I" exists inside of the string, so this would also return true.

Whereas, like if I did an "A," well, the "A" exists, so this is also going to return true.

So, it's going to give us a lot of results, but the better the string is that you type, then the less results there's going to be, obviously, and the more accurate this search will be.

So, this is just one way to search.

You can check if this string is in this other string.

However, we're going to convert this to lowercase, and we're going to convert the name to lowercase as well just to make sure that we're not dealing with any capitalization when we're searching for the name.

So, this way, the string we're looking for is lowercase, and the string we're looking inside of is lowercase as well.

So, if that's the case, we're going to say `results.append(person)`.

And then we want to print out all of the people.

Now, guess what?

We already printed out all the people using this, so I'm actually going to copy this, and I'm going to put this in another function.

I'm going to say `display_people(people)`, and this is going to take in people.

We're just going to print this, and now we can replace all of this here with that function.

So, I can say `display_people(people)` and then I can do the same thing here.

So, rather than passing people, I'm going to pass my results.

So now it will display all of the results that we found.

Okay, so that's how the search function works.

It's just going to display the results that match this search.

So, let's go here now to search.

Let's call the function again.

We need to pass our people list, so let's make sure we do that and let's test out the search.

And if we want to make this a little bit easier, we can just start with some people so we don't need to always keep typing them in.

So, I'm going to say `people.append({"name": "Tim", "age": 25, "email": "tim@gmail.com"})`.

And then let's do another person that has like a Tim, so we'll go like `people.append({"name": "Timothy", "age": 27, "email": "tim2@gmail.com"})`.

And then let's go with someone completely different, so we go like `people.append({"name": "Joe", "age": 34, "email": "joe@gmail.com"})`.

So now let's run the code, and it says "Hi, welcome to the contact management system."

Contact list size is three.

So, let's try to search, and we're going to search for a name, and I'm going to search "TI."

When I do that, you can see that we get Tim and Timothy, and then it says contact list size.

You can add, delete, etc.

Okay, let's try to search again.

Let's search for Joe, and you can see that Joe pops up, and we're able to search through our list.

So, let's quit now and get out of that.

Now, I just want to make this a little bit neater, so I'm going to do a print here.

Yeah, just going to print before this so that there's a little bit of separation between the last command.

Okay, so that's it when it comes to adding, deleting, searching.

But what we want to do is actually store all of our contacts in a text file so they don't get deleted as soon as we stop the program.

Because it's really annoying that we add a bunch of contacts, we delete them, whatever, and then they're gone forever.

So, we need the ability to save this to a file and then read this in, and I'm going to show you how to do that using something known as JSON.

So, we're going to go to the top of our program, and we're going to import the JSON module, which is built into Python.

Now, JSON stands for JavaScript Object Notation, and it allows us to save kind of objects, which are very similar to dictionaries, in a file, and we can do this quite easily.

So, firstly, we want to create a new file, and we're just going to call this file `contacts.json`.

Okay, the code that I'm about to write is only going to work if this exists, so just make sure that this exists.

Now, inside of here, we're going to add an object.

So, we're going to create this object, and we're just going to say `contacts = []`.

Notice this looks exactly like a Python dictionary.

I have an entire video on JSON.

I can put that on screen if you guys want to check it out.

I'm not going to explain all of the syntax here, but it's a way that we can store different kinds of Python objects, in this case, dictionaries.

Okay, so we have contacts.

Make sure it contains this inside of here.

Now, for people, we're going to remove people, and we're going to load in this JSON file and grab the contacts that exist.

So, how do we do that?

Well, we can do that like this.

We're going to say `with open("contacts.json", "r") as f:`.

Now, this is just the way that you open a file.

Okay, you say `with open`, you put the name of the file, make sure it's in the same directory, and that it's named the same thing as what you have here.

And then we put the mode.

When you put "r," it means we're opening it in read mode, so we can only read to it; we can't write from it.

Then we are going to say `people = json.load(f)`, and we're going to load this file.

Okay, so when you do `json.load`, you can actually pass a file to this.

It will then load the file, and it will parse this as JSON, and then I can do this and access the contacts key, which will give me this list from the JSON file.

I understand this isn't entirely clear, but that will give us our people.

Okay, now the way to test this is to go in here and add a person.

So, we're going to go name Tim, age 24, email tim@gmail.com.

Save that, and notice that it kind of gives us some nice formatting, and this is the exact same thing that we did within Python.

We're just now putting it in this JSON file.

Okay, so we have people equals `json.load(f)`, and then we're accessing the contacts list.

Let's try this out and see if it works.

Okay, so you can see it says contact list size one.

You can add, delete, and search.

Let's just go to delete because it will show us a list, and you can see that Tim exists.

And let me just get out of this by Ctrl + C.

Okay, so now what we want to do is be able to save to the JSON file.

Now, saving to the JSON file is actually quite straightforward.

I'm just going to copy this, and at the end of my program, so as soon as we decide to quit, so after all of this, what I'm going to do is open this again in write mode.

When you open a file in write mode, it's going to delete anything that already exists in that file.

So, make sure you only do this if you're like creating a new file and you know what you're doing.

So, we're saying, "Okay, we're going to open this file in write mode," which is going to clear everything inside of it.

Then we're just going to write into the file our new people.

So, we're going to say rather than `json.loads`, so inside of here, we're going to say `json.dump({"contacts": people}, f)`.

We're going to dump into `f`, which is our file object, and we're going to dump the following: we're going to say `{"contacts": people}`, which is a list.

Okay, so we have our list of people that we load in, and then we say `json.dump`, we're dumping into this file the new people.

So, whatever the modification is that we've made, so that just means as soon as we quit, we're just automatically going to go here, and it's going to save whatever work we've done.

Okay, so let's run this and see if it works.

We're going to go add Joe, 43, joe@gmail.com, and then it says person added.

Now, let's quit, and we got an issue.

It says "TextIOWrapper not JSON serializable."

Okay, give me one sec, guys.

Let me see what the problem was.

Okay, so silly mistake here.

We just need to pass the content and then the file, not the file, then the content.

I just had these in the wrong order, so let's just swap those around.

This is now going to be empty, so we need to just fix this.

So, we're going to go `{"contacts": []}` just because I messed up, so it deleted the file.

Here, before it actually wrote inside of it.

Anyways, what we're going to do now is run this again.

We're going to go add and go Tim, 24, tim@gmail.com.

We're going to add again, go Joe, 23, joe@hotmail.com.

And now let's go quit, and then if we go to contacts, you can see that now these contacts have been added.

Okay, so now if we run the program again, so let's press run, you can see that the contact list size is two.

So, it's persistently keeping track of all of our contacts.

Now we could delete, so let's delete Joe.

Okay, and then quit, and if we go back here to contacts, you can see now that Joe has been deleted.

Okay, perfect.

So, there you go.

You now have a persistent contact list project.

This was a lot longer than I was expecting it to be, to be honest with you, but all of this code will be available from the link in the description, so feel free to check it out.

Okay, so let's get started with our fourth project.

Now, this project is going to be like tic-tac-toe or X's and O's.

Now, in order to start this project, we're going to need some kind of board.

The board is going to represent the different rows and columns that we have and all of the positions we have.

And this is what the board is going to look like.

Now, notice that I actually have lists inside of lists.

Now, this is referred to as a two-dimensional list or a multi-dimensional array, multi-dimensional list, nested list.

There are a lot of different names for it, but the point is this is fine.

We can put a list inside of another list.

Now, if I were to do something like access `board[0]`, what that gives me access to is this first list because that's the first element in this overall list.

Then if I access something like `position[2]` on `board[0]`, which by the way is valid syntax, that's giving me access to this, which is the second index in the first list.

Okay, or the zeroth list, whatever you want to call it.

So, just keep in mind that this is kind of how you do multi-dimensional array accessing, and you can go a step further, right?

Like, I could add another list here and then access that.

I can add elements to it, all kinds of things you can do, and it'll make a bit more sense as we go through some examples.

But I just want to illustrate that this is like a valid thing to do.

So, these are the different rows, and then each element in here is one of the values in a specific

At the end here, we need a separator because we don't want that; we just want it in between. Then, we're going to print out the string that we created, which should display the board. We can test that by calling the print board function.

Okay, we're going to pass our board and let's just put in some X's and some O's to see what it looks like. Let me zoom out a little bit because now the code's getting a bit larger.

Let's run it, and you can see that this is our board. Now, obviously, we have too many dashes, so we can remove a few of those. Let's get rid of maybe five dashes. Let's print that again, and that looks significantly better to me.

However, you can see that my X is kind of squished to the side of the screen, so we may want to add a little bit more space there. To do that, we can say if J is equal to zero, then we're just going to add a space to our row string. That should give us that separation.

Now, you can see that X is spaced out a little bit and it's not attached to the edge. Great! That looks good to me. Let me close the terminal and let's move on to the next step, which is going to allow the user to insert something into the board.

We're going to define maybe get_move. We're going to take in a turn; the turn will be whether this is an X or an O that is moving. We're going to take in the turn and the board.

Now, what we're going to ask the user for is a row and a column where they want to place this element. So, we're going to say row is equal to input and we're going to say "row" like this. We're then going to say col is equal to input and we're going to get the column.

Now, for the row and the column, we're going to convert this to an integer. In this case, we're just going to assume that they give us valid input, meaning that they give us a number. We could check if they gave us a valid number, but we've already done that a bunch of times, so I don't want to keep repeating that.

So, I'm just going to convert this directly into an int. Again, we're going to assume that they always give us an integer. If they don't, this code is going to break, but you can implement that if you want and use some of the code from the previous projects.

Now that we've got that, we're going to make sure the row and the column are valid within the bounds of what they need to type. We're going to say if row is less than one or row is greater than the length of the board, then we're going to have to ask them to enter another position.

So, let's make this a while loop. Let's go while true, and we're going to say row, col, and then we're going to say continue and print "invalid row, try again."

When we do the continue, it's just going to bring us back to the top of the while loop. Now, we need to do the same thing for the column. So, I'm going to say if the column is less than one or the column is greater than the length of the board at the row they selected, then I'm going to say print "invalid col, try again."

Again, we're going to go with a continue. Then, down here, we are going to make sure that they did not try to place this in a position where we already have an element.

The first thing I'm doing is checking to make sure they give me a row and a column that actually exists inside of this board. They shouldn't give me row 25 and column 7 or column -2 because if I try to use those later on, that's going to give me an issue.

Now that I've checked that, I'm going to make sure that within the board, there's not already something there. I'm going to say board at row, col. I'm going to access whatever the row is and then access the column within that, and I'm going to see if this is not an empty string.

So, I'm going to say if board at row, col does not equal this empty string here, then I'm going to say print "already taken, try again."

We can go with a continue here as well. Actually, if we want to avoid doing all these continues, we can just run these as elifs. So, I can just make this an elif, and then we can do an else.

Here, we can say break because that means they actually entered a valid position.

So, this has collected the input, right? We've said let's get a row and a column, we'll convert those into integers because by default, they're going to be strings. We'll make sure the row is valid; if it's not, they're going to try again.

We'll make sure the column is valid; if it's not, they're going to try again. We'll make sure there's not already something in that position. If there is, we try again.

We come down here to the break. When we hit the break, that means none of these things were true, which means it was valid what they gave us for the row and the column.

Now, when we come outside of the while loop, we can actually insert whatever their character was, like the X or the O, in the board. So, we can say board at row, col is equal to the turn. The turn will just be like the X or the O.

Now, let's try this out. We're going to say get_move. We're going to print the board, and we're going to get a move. First, we'll get it for X by passing the board, and then let's do the same thing for O.

Okay, O and then board, and let's print the board after all of this so that we see what happens. You can see the usefulness of functions, right? How I'm reusing these functions multiple times to build my project.

Let's try this out. You can see that it asks me for a row. So, let's go row 1. Actually, sorry guys, let's quit out of this because I realized we made a mistake.

When they enter the row and the column, we're going to subtract one from them when we try to access them in the positions. The reason for that is if they type row 1, they really mean row 0, right? Because we're talking about the indices in the list.

The same thing goes for column 3; they really mean column 2. So, we're going to make sure we do that subtraction in all the places where we access these elements so we don't get an index error.

Okay, so let's rerun. We're going to go row 1 and column 2, and you can see now it places the X.

Now, let's go; we obviously want to block this. So, row 1, column 3, and then it places the O.

Now, let's try some cases where it doesn't work. Let's try to go in maybe 1, 1. "Already taken, try again." Okay, good, so that's working.

Let's try to go in 4, 2. "Invalid row." Okay, let's try to go into 5. "Invalid column."

Okay, so you get the idea. Now, let's go row 1, column 2, and let's go maybe row 3, column 1.

We got an issue here; it says "issue with the board." Let's see what the problem is here.

Okay, so let me debug this in real-time with you. We went row 3, column 1, and it says we're getting an index out of range at this line.

The reason we're getting the index out of range is that we tried to access the length of the board at the row that they typed in. However, the row that they typed in, we did not subtract one from because they typed in 3.

So, that means we tried to access a row that did not exist because the index was out of range. We just need to make sure we subtract that one.

Great! There are other ways to write this code; this is just me writing on the fly.

Let's just try that one more time with row 3 to make sure that this works. So, let's go row 3, column 1, and you can see it does indeed work.

Let's just fill the rest of this in, and you can see that it gets placed. Okay, so that's working well.

Now, what we want to do is start with the board empty and implement something that allows players to go in turn. We just keep going in turn until either someone wins or until we fill up the board.

Okay, so we've emptied the board. Let's get rid of this and do a while true again.

Actually, we can do while true; that's going to be fine. Inside of here, we're going to alternate the turns.

We're going to start by the turn being equal to X, and then we'll alternate it to O and keep going from there. While we're at it, we'll also have the turn number equal to one to start, and as soon as we hit nine turns, if no one has won, then we know that it's a tie game.

So, we have while true, and we're going to say the following. We're going to say print "It is the" and then we can go with the turn. "Player's turn, please select your move."

So, we're saying, "Hey, it's the X player's turn, please make your turn." It's the O player's turn, whatever it is.

So, they're going to make their turn. We are going to say get_move, and we're going to move with X. This is going to be the turn, and then we're going to pass our board.

We're then going to print the board. So, we're going to say print board, and we'll print it.

Actually, sorry guys, we'll take this out and we'll just print it once here. The reason for that is after each turn, we'll print it one time.

We don't want to print it before and after, but we do want to print it once before the game starts so the players can see what it looks like.

Let's make this a little bit larger so we can read everything.

Okay, so now it should just go, and then after they do their turn, we're going to alternate the turn.

So, we're going to say turn is equal to, and we'll actually have to do a quick if statement. We'll say if turn is equal to X, then turn is equal to O.

Or if turn is equal to O, let me make sure that's an O, not a zero. Okay, good.

Else, turn is equal to X.

Okay, so this will just flip the turn from X to O, so it keeps alternating each time.

Then, we're going to say turn_number plus equals 1.

We're actually going to do a quick change to the while loop here, and we're going to say while the turn_number is less than nine.

As soon as the turn_number becomes nine, it means that we've actually tied the game.

So then, at the end here, we can say print "tied game."

Okay, getting somewhere. So, let's run this.

It is the X player's turn; please select your move. So, let's go 1 and 1.

Okay, it is the O player's turn; please select your move. Let's go 1, 2.

Okay, let's go 1 and 3. Let's go with maybe we want to go in the middle, so 2, 2.

I'm just going to keep going and just make sure this works. I'm going to go 2, 3. I'm going to go 2, 1.

I'm going to go with a 3, 1, a 3, 2, and that would win the game, but obviously, we haven't implemented that yet.

Then, we're going to go 3, 3, and it says "tied game" just because we got to the end, but at least the game ended.

Sweet! Now, also, just because I want this to be a little bit cleaner, let's do another print statement here just so it separates things out a little bit and makes it easier for us to see.

Okay, so now I guess really the last thing to do is just to check who's winning, and then we could add the AI component if we want to do that.

To be able to check who's winning, it's a little bit more complicated, so we're going to write a function here.

We're going to say define check_win. Now, we need to know the board, so let's take in the board.

What I'm going to do is define all of the lines that we have. I'm going to make lists of lists, and these lists are going to contain tuples.

Now, a tuple is a new type that you haven't seen before. It works pretty much the exact same way as a list, except you can't change it.

So, once you define it, you can't add a new element to it, delete an element from it, etc. It's simply just meant to store things like coordinates or positions, which is what we're using here, or things that are grouped together but aren't meant to be mutable.

A tuple is what's known as immutable. Again, we haven't really got into that concept too much in this video, but it is worth noting.

This is an immutable type, while a list is mutable. Like a list, you can increase the size of a tuple; you cannot.

Anyways, what I'm going to do is define all of the positions of lines. If we look at the board, we have eight possible ways to win, right?

You can go this row, this row, this row, this column, this column, and it's hard to do the columns here.

So, we have three rows and three columns that you can get in, and then two diagonals. I'm just going to define all of those lines here, and it's going to be a really easy way for us to check if we've won or not.

So, I'm going to go with my row-column notation with indices. The first one we'll do is just the first row: 0, 0; 0, 1; and 0, 2.

The idea is I just write all of the lines that could potentially win, and then I'm going to check if all of these lines are populated by the same player.

There are more efficient ways to do this, but this is a really easy way to write it, so that's why I'm going to do it here.

Next, we're going to do the next row, so it's going to be 1, 0; 1, 1; and 1, 2.

Now we have another list for the next line. Each line goes in its own list.

We're going to have 2, 0; 2, 1; and 2, 2.

Let's save that; it should get automatically formatted for me.

Now, I'm going to go with the first column, so it's going to be 0, 0; 1, 0; and 2, 0.

Now we need the next column, so this is going to be 0, 1; 1, 1; and 2, 1.

Okay, I should not have thought that hard. All of these are 1, and then the rest just go up by 1, 2, 3.

Lastly, it's going to go 0, 2; 1, 2; and 2, 2.

Okay, so that's the last column. Now we need the diagonals.

The diagonals are going to be 0, 0; 1, 1; and 2, 2.

Then lastly, we're going to have the opposite diagonal, which is going to be 0, 2; 1, 1; and 2, 0.

Okay, I think that's correct. We will see if that's correct or not once we start implementing this.

Now, what we can do is check if any of the wins exist.

To do this, we're going to add another parameter called turn. Now, turn is going to be the player that just moved, whether it was an X or an O.

We're just going to check if the player that just moved won because if we always check if the player who just moved won, we never need to check both of them.

We can just check one at a time because we'll know if X just moved and we had already checked if O won and they didn't win, then the only person who could win right now is X.

Hopefully, that makes sense, but that's what I'm doing here.

Then, I'm going to say for line in lines. I'm going to get every single line, which is one of these individual lists.

Now, I'm going to check if all of the positions in my board are equal to the same thing, which is turn.

So now I'm going to say for position in line, and then I'm going to say row, column is equal to position.

What this will do is break down this tuple into its row and column. This is called tuple or list decomposition, and it's a quick way to grab two values out of something like a tuple or any iterable object.

So, we're grabbing the row and the column, and now I'm going to say if board at row, column does not equal turn, then what we can do is break out of the for loop.

Now, we will break out, but we're also going to add something here that says win is equal to true.

Then here, we're going to say win is equal to false.

Now, this is going to be a little bit weird, but what I'm doing is setting up a flag.

The idea is that for each line, I'm going to assume that we have a winning position. What I'm going to do is simply validate that it's not a winning position in this for loop, which means if it doesn't say that it's not a winning position, it is a winning position.

So, we're saying win is equal to true, and then we're going to go through each position in our individual line.

Notice this is set in the for loop, so for each line, we're doing this. We get the row and the column and check if whatever is at this row and this column is not equal to whatever we're checking.

In this case, X or O. If it's not, we're going to say that the win is equal to false and we're going to break.

Now, outside of this for loop, we're going to say if win, which means if this is true, then return true, which just means we won.

Otherwise, at the very end, we're going to return false.

So, this is one way to write this. There are a lot of other ways to write it as well, but the point is we're looping through each line.

We're assuming that we're winning, and we're going through here. If we're not winning, we're going to set it equal to false.

Now, if we don't set win equal to false, that means that every position we checked wasn't the case, which means this was true.

Like the board at row and column was equal to the turn, which means we have a completed line, which means we won.

So, we should return true, and that will immediately exit this for loop for us.

Otherwise, if we get through this entire for loop, so every single line, and someone didn't win, we return false, and we know there's no winner.

Now that we have that function, we can use it in here. After the turn, so after the board is printed, we're going to say winner equals check_win(board, turn).

Now, at this point, if winner is true, it means whoever just went won the game.

So, we can just do a simple print statement here, or actually, we can just simply break out of the loop.

So, we can say if winner, so if someone's won, break.

So, we're not going to execute this, and then rather than printing "tied game," we can print that someone won.

So, we can say print "the winner was" and then we can go with whatever the turn was.

So, we can just say turn.

Okay, because what's going to happen is if we won, then the turn did not change because we didn't do this.

So, we'll just say the winner was whoever last moved because they triggered this to win.

However, we still need to handle the tie situation.

So, we're going to say if the turn_number is equal to 9, then we'll print "tied game."

Otherwise, we will print this.

Okay, so the point of me doing this is that if the turn_number reached nine and that was the reason why we broke out of the for loop, then we tied.

But if it's not nine, and it won't be nine if we won because we didn't increment this here, that means that someone won.

So, we print the winner.

So, that's the basics of how I'm checking that.

Let's try this now because that pretty much wraps up the basic functionality at least.

It says, "It is the X player's turn; please select your move."

Okay, so let's go try to win quickly. So, 1 and 1.

Okay, it's the O player's turn, so let's go 2, 1.

Let's go 1 and 2.

Let's go 2, 2.

Let's go 1 and 3, and it says "the winner was X," and that is correct because we won.

Now, you can try every single combination here and see if there are any bugs, but I'm going to assume that this is working.

So, now that we've done that, it's time to add the AI.

Now, I actually kind of want to leave this as a challenge for you guys because I don't think you're going to get that much value from me just writing it out.

What I will tell you is this: you can just always tell one person to go first, so maybe X is always going to be the player turn.

Now, what you're going to do is rather than calling this get_move function, you're pretty much going to do something like this: you're going to say if the turn is equal to the computer, which in this case we can just assume it's O, then we're going to do computer_move.

We're going to call that function; otherwise, we're going to call this function.

Now, nothing else changes. All you have to do is write this function, the computer_move.

So, write a function called computer_move that takes in the board and takes in the turn, so whatever the computer's turn is.

What you want to do in there is check if there's any position where if the other player were to move into it, they would win.

Again, the idea is very similar to this function, except you need to check if there's one position where if the player had moved there, it would cause the game to be won.

If that's the case, you move there.

Now, you can also do the same thing for yourself. So, you can check, "Hey, if I were to move into this position, will I win the game?"

So, that's the basic algorithm. I'm not going to say it's easy, but I do want to leave it as a little bit of a challenge.

Maybe someone can leave a comment with the answer, and I can pin that down below.

Remind me, guys, in the comments if I don't do that, or I can code it up if no one leaves the answer.

But I do want to give you a bit of a challenge rather than just revealing it to you.

So, that's what you want to do for computer_move.

Now, let's move on to the next project.

So, now we're moving on to the fifth project. This is going to be a random quiz generator and grader.

Now, I won't lie to you; this project is not necessarily that much more complicated than the last one or even the third project that we've done.

But I do want to leave this a little bit open to you guys to treat as a challenge to yourself and bring everything full circle because it's definitely very doable with the knowledge you've learned so far.

Anyways, I will code it out, but I am going to explain what you should do, and maybe you can attempt it on your own first before watching through the video.

What I want to do here is have a bank of questions. Ideally, I want to store those in something like a JSON file, something like questions.

So, we have all these different questions, and I want each question to have the question text, options like 1, 2, 3, 4, and then I want you to be able to select the different options, A, B, C, D, whatever you want it to be.

Now, I want the program to automatically generate a random quiz by sampling random questions from my questions list.

So, maybe you have a question list of 10, and we want to create a quiz that has five questions.

Well, I want five different questions each time. They don't necessarily need to be unique, but I want just a random sample of them.

I'll show you how to do that in case you don't understand random in Python.

Anyways, that's the first part, and then we want to be able to grade the quiz to know if the user got the answer correct or incorrect, give them maybe a percentage score, etc.

Okay, and then maybe we can store all of their scores in a scores table and potentially time how long it takes them to do the quiz.

Okay, so let's build that out. If you want to try to build that, feel free to pause and build it out. Use your imagination for any of the requirements that I haven't explained, and maybe you can even skip to the very end to see how the project is working, and then you can try to code it out if you want.

Again, I haven't coded this yet, so bear with me.

Alright, first thing we're going to do is import random, and we're also going to import JSON because we'll use that to load some files.

Now, random is a module that does what it says: allows us to generate random numbers and various things like that in Python.

So, first step, let's get a question bank. Let's start with it in Python and load it as a JSON file.

So, we're going to say question_list is equal to, and we're going to make a list. Inside of here, we're going to have some questions.

Now, the questions will have a structure that looks like this: we'll have "question," we'll have some text, we'll have "options," we'll have a list of the various options, and then we'll have "answer," and that will be equal to the answer.

Let me generate a quick list of questions here, and then I'll be right back.

So, I've got a long list of questions here. If you want the same ones, you can just copy them from the GitHub link in the description.

You can also use something like ChatGPT to generate questions like this, which, fun fact, is exactly what I just did.

That's why I took a quick pause. You can just go and say, "Hey, generate a list that looks something like this," give it a sample question, and it will just give you a bunch.

You can classify what type of questions you want, categorize how many you want, and just ask it whatever you want it to do. It's really good at generating sample data.

So, I've got all of this quiz data. Now, the first thing we want to do is randomly sample it and select a bunch of different questions.

Now, while we're here, let's actually just load it from a JSON file because this is taking up a ton of room in our Python file.

So, let's make a new file here. Let's call this questions.json.

Okay, let's copy this and put it inside of there. We need to make a slight change, but it should be pretty straightforward.

So, let's go with questions, and what we're going to do is start by adding an open brace and we're going to say "questions" like this is equal to the list.

Make sure you spell "questions" correctly, and then just throw a brace at the very end here.

The reason for that is you can only store dictionaries in here; you can't store lists. You need to store a dictionary, so I need to make this a dictionary by having a key associated with this list as the value.

Okay, so now if we want to load that, let's just make a function that can load that for us.

So, we're going to say define load_questions.

Okay, and inside of here, we are going to load the file. So, we're going to say with open, and then this is going to be questions.json in read mode as F.

We're going to say questions are equal to json.load(F), and then we'll access the questions and we will return them.

Okay, so that should give us all the questions we need.

So, let's just test this quickly. We're going to say questions is equal to load_questions.

We're then going to print out the questions and just make sure this works.

So, let's see. We got an issue here: "expecting property name enclosed in double quotes."

Okay, so let me see where the problem is here.

JSON.load, we're loading this file questions.json. It's possible there's something that maybe is in single quotation marks, but I'm not exactly sure what that is.

Let me just remove some of these trailing commas and see if that potentially fixes the problem because sometimes if you have these trailing commas, you can see they're kind of highlighting in red.

I can give you an error; it hasn't usually caused errors for me in the past, but let's try this.

I'm kind of getting some live debugging here.

Okay, remove that, remove that. Almost done.

Remove that, that, just a few more.

That, that.

Okay, last ones.

And save, and let's just run this again.

And there you go; we get all of the answers.

Okay, so it was actually trailing commas. I'm surprised that was the issue, but that was the problem.

So, get rid of all of those commas, and now you can see all of the questions have been loaded in.

So, now we want to get a random sample of those questions.

To do that, we're going to say define get_random_questions, and we're going to take in the questions and the number of questions.

Number of questions will be a number, and that's going to tell us how many we want.

Now, to grab the number of questions, first of all, we're just going to do the following: we're going to say if the number of questions is greater than the length of the questions that we have, we're just going to say that the number of questions is equal to the length of questions.

So, we don't end up getting an index out of bounds error.

Then we are going to say questions, and we can just do maybe like random_questions is equal to random.sample.

Now, sample can randomly select a sample from an array or from a list in this case.

So, I can say questions, and then I can put the number of questions, and this is just going to give me this many random questions from this list.

Now, obviously, if the number of questions equals the questions, we're going to get all of them. Otherwise, it'll just randomly pick a set of them.

That's it; that's how that works.

Okay, so pretty straightforward, and then we can return the random questions.

Now, what we can do is load our questions.

We're then going to say random_questions is equal to get_random_questions.

We're going to pass our questions, and let's pass maybe two, and let's print out random_questions this time and just see what we get.

And you can see we get "What is the boiling point of water?"

Okay, let's run it again, and notice we get different questions every single time.

Now, sometimes we may get the same ones, but it is random.

Okay, so there you go; we now got random questions.

We've loaded the questions in, and the next thing to do is to ask the user the answer to the questions.

Okay, so let's do that. Let's make a function that will print a question, or maybe we can call it ask_question.

This is just going to take one question.

Okay, so we're going to take one question like that.

Well, to ask a question, we simply need to print out the text, so if we go look at this, and then the options, and then ask the user for the answer.

So, let's do that. We're going to say print, and then we can go with question.

I believe it's called question; we can check.

Yes, that's what it is.

Okay, so that's going to print that out to start, and then we can say for i, option in enumerate, and we're going to go with the question, and then this is going to be the options.

We're going to print it like this.

We're going to say print str(i + 1) + "." + " " + option.

The reason for this is I just want to print out kind of like 1. 2. 3. 4. and then the options so the user knows how to select the option.

Then what I'm going to do is get the user to type in the index of the option that they want to select.

So, I'm going to say number is equal to input, and again in this case, we're going to assume that they just give us a valid integer.

So, we're just going to say int and then the input.

If they don't give us a valid integer, the program will crash, but again, I've shown you how to handle that later.

Then what we're going to do is make sure this is within the range.

So, we're going to say if the number is less than one or the number is greater than the length of options, then we're just going to return false.

This is just going to indicate the user got it wrong, and we're just going to print something like "invalid choice, defaulting to wrong answer."

So, pretty much, hey, if you don't give us a valid choice, sorry, too bad, you got it wrong.

That's what we're going to tell them.

Now, otherwise, we're going to check if they got it right.

So, we're going to say else.

Actually, we don't really need an else because when there's a return, it means that we're just going to exit the function.

So, if we go here, if we didn't return, it means this wasn't true, so we can just write code directly here.

So now I'm going to check if this was correct.

So, I'm going to say correct is equal to, and then this is going to be the following: we're going to say question["options"][number - 1] because, again, remember they're going to be selecting one more than what it actually is in terms of its index.

We're going to check if that's equal to question["answer"].

Okay, so we're getting the options, we're looking at the one they selected, and we're seeing that that's actually equal to the answer.

Then we're just going to return correct.

So, that's going to tell us whether or not they got this question correct.

Okay, so that is going to ask one question, but obviously, we want to ask all of the questions.

So, let's do that now.

We're going to go down here and we're going to say for question in random_questions, and then what we'll do is we'll type ask_question like that and then pass the question.

Okay, so let's try this now.

Let's run the code.

It says, "What is the hardest natural substance on Earth?"

Let's go with 2, and we got an issue.

It says, "list indices must be integers or slices, not strings."

Okay, so the problem we're getting here is what is that?

Okay, so I just realized that I have questions here rather than question, so that was the issue we were getting because we were trying to access options on the questions list rather than the question dictionary.

So, let me just run this again and go with, oh, we got a different one now.

What is the largest planet? I feel stupid for not knowing this.

Let's go with Jupiter.

I don't know if that was correct or not, but you can see it moves to the next question.

What is the boiling point of water? That is 2, and there you go.

Okay, so there you go; we answered the questions.

Now, the next thing to do is let's just add a little bit of separation between our questions and keep track of if we got the question correct or not.

So, to do that, we're going to say correct is equal to 0, and we're going to say if is_correct, then correct plus equals 1.

Okay, so we're just going to add one to the number of correct questions.

That way, when we get down to the end here, we can say print summary like this, and we can say print "total questions," and then we can print the total questions.

So, let's make another variable and say total_questions is equal to, and let's actually get that from the user.

So, let's say input "enter the number of questions."

Okay, and then we're going to use total_questions here when we generate the random questions.

Here, we're going to put total_questions.

We then are going to get whatever their score was.

We're going to say print "correct answers," and then that's going to be the number that they got correct.

Lastly, we're going to say print "score," and we are going to take the correct, so let's do this in parentheses, and we are going to divide this by the total_questions and then multiply that by 100.

That will give us the percentage.

Okay, so also, we probably want to add the percent sign.

So, let me do this plus "%" and we're going to convert this all to a string.

I know I'm going fast, guys, but it's already because we've covered a lot of this stuff already.

Okay, so that should actually finish the quiz.

I'm going to add some more stuff to it, but let's quickly run this.

So, under the number of questions, let's go with 3.

Select the correct number: "What gas do planets absorb from the atmosphere?"

I actually don't know the answer to that one.

"Which planet is closest to the Sun?"

I also don't know the answer to this.

You guys are going to think I'm stupid.

Let's go with Mars.

"Who wrote To Kill a Mockingbird?"

I think it's Scott Fitzgerald.

Okay, we got total questions; only got one correct.

Okay, that's great.

Answer for me there, and we scored 33.33%.

So, let's round that off.

To round this off, I'm actually going to use the round function.

So, we're just going to add another set of parentheses around this whole thing, although we can't.

Yeah, it needs to be within the string.

So, I think that's okay.

So, we're going to round this, and we can actually specify at the end here that we want to round this to two digits.

I know it's a little bit weird to read, but round is kind of these blue brackets right here.

The rest of this is what's being rounded; this is the number of digits to round to.

Now, if we run this, let me get embarrassed again.

Let's go with 2.

"What is the hardest natural substance on Earth?"

Okay, I know this one; that's diamond.

And that ran in 2.16 seconds.

Sweet!

So, that's pretty much all I'm going to do for this quiz project.

However, if you want a challenge here, what you can do is try to save all of the scores that users get, and you can create a leaderboard to see who answered the fastest, who is the most correct, and what questions maybe are the most difficult people are having challenges with.

You could store that in JSON as well, and I think that would be a cool way to extend this project.

Anyways, though, I am going to wrap it up here.

If you guys enjoyed the video, make sure to leave a like, check out all of the code from the link in the description, and I will see you in the next one.

[Music]