Python, it's one of the world's top programming languages. In this course, you're going to learn the basics of Python from Treasure Por through a bunch of interactive lessons and projects. So, this is a great beginner's Python course. Hello and welcome to Learn Python, a course about one of the most in- demand languages on the planet. Python is immensely popular and it's everywhere. NASA uses it to analyze data from space missions. Open AAI builds its AI systems with it. Netflix uses it to decide what you're going to binge next. Python is powering the ubiquitous apps that you use every single day. Apps like Instagram, Spotify, Google, and Dropbox. If you're looking to get hired as a developer, data scientist, AI engineer, or really any technical role, knowing Python is your superpower. And Python isn't just popular because companies love it. It's popular because developers love it. The syntax is clean and the code reads almost like English. It was built with elegance and simplicity in mind. And so with Python, you get to spend less time fighting the language and its quirks and more time actually building things. It's powerful enough to run production systems at scale and approachable enough that it's the go-to first language for beginners all over the world. In this course, you're going to learn by doing. You'll build real working apps, solve many challenges, and write code from the very beginning. Everything we'll build will run right here in the Scriber editor where Python is already set up and ready to go. So, are you ready to learn some Python? Let's get into it. This section is all about learning the basic building blocks of a Python program. And we're going to do that by building a fun and useful app called PayU. PayUP is an expense splitter. So, think splitting the dinner tab, dividing up costs for a group trip, or settling up after a night out. Let me show you what we're building. The app gives you a nice greeting, and then prompts you for four things. The occasion, so we'll say the occasion is Paul's Pizza. How much the expense was, we'll say $345. If there was a tip or a service charge as a percentage, we'll say 20%. And finally, how many people were in your group, we'll say eight. Once you've entered all the info, the app prints a clean breakdown, and that includes a summary of the cost. It calculates the service charges based on the percentage, the group size, the grand total, and finally, of course, what each person must pay up, what each person owes. Along the way, you'll learn about writing and running Python programs, variables and the print function, strings and string formatting, capturing and handling user input, data types and type conversion, and basic math operations, as well as completing a ton of practice and coding challenges. So, you'll build up to the PayUP app by working through smaller challenges that introduce each concept. By the time we start writing the app, nothing will be new. You'll just be putting the pieces together. Let's jump into it and get ready to start building. Before we start building, let's poke around in a working Python program and get comfortable reading and changing code. Here we have a Python file called script.py and in it is some code for a party invitation generator. It formats and prints a personalized invitation based on the information that you give it. Let's run it. Open the terminal with command J. Type Python script.py and hit enter. And there's our invitation. There's a title. There's the body of the invitation, the host, the date, and address code. All formatted and printed. Now, let's close the terminal for a second and look at the code. You can close by pressing command J again or using this arrow to toggle the terminal open and closed. At the top of the code file, you've got host, date, dress code, and name. These are called variables. And a variable is just a label that holds on to a piece of information. And you can use that label anywhere you want in your program. As you can see, each variable corresponds to a place in the invitation marked with curly brackets. So, we have host, host, date, date, dress code, dress code, and name. Watch what happens when I change a variable at the top of the file. We'll change the name to Emily. Now, when I rerun and rather than typing Python plus the file name again, I can just press the up arrow to get my last command. When I hit enter to rerun, the invitation updates automatically because everywhere that we use the label name, Python swapped in the new information. And this is really the core idea of variables. They let you separate your data, meaning the information that you might want to change, from the rest of your code. So now it's your turn to try it. Into the file, I'm going to copy and paste a challenge. One quick thing before we get into the challenge. Notice that the challenge instructions each start with a pound or hash symbol. That tells Python to ignore that line when the file runs. When something has a hash or pound symbol in front of it, it's called a code comment. You'll use comments to leave notes for yourself, explain what something does, or just think out loud inside your code. Python will skip right over these lines and ignore them completely. So now let's read the challenge. Step one is to change the program's variables so that the invitation prints your name and today's date. Step two is change the dress code to casual. We're staying in to learn Python. No need to get dressed up. And finally, don't forget to run the Python file to check your work. To run a Python file, you'll want to come in here to the terminal and type Python plus the file name and then hit enter. Go ahead and give this a try. All right. Hopefully that went well. Let me show you the solution. First, we're going to change the invitation so it prints my name and today's date, which we'll say is October 31st. Next is to change the dress code to casual. And finally, I'm going to save. Open up my terminal. We'll clear this out so that we can see clearly what's going on. And if you weren't aware, if you ever need to clear out this window, you can just type clear and hit enter and everything goes away. Now I'll type pythonscript.py to run the file and you'll see my name has changed, the date has changed, and the dress code has changed. Very excellent work. Hopefully you've had some fun playing around in some Python code. Feel free to stay here and keep experimenting. When you're ready, meet me in the next scrim. To build the payup app, we need a way to store information in Python. Things like the total cost, the number of people splitting it, and the tip percentage. That's where variables come in. A variable stores a value under a name you choose so that you can use that value later in your program. Here's the syntax. We'll say meal total equals 85. The name or the variable is on the left and the value is on the right and the equal sign is what connects them. This is called an assignment. You're assigning the value of 85 to the name meal total. Let me show you what happens if I print meal total. Python recalls the value and prints it. And if the total changes, I only have to update that value in one place. So I'll change 85 to 120. And then I'll run the program again. And as you can see, same name, but the value has updated. And that is the core idea of a variable. So now I have a challenge for you. Your challenge is to think of the last time that you ate at a restaurant. How many people were you with? Save that number to a variable called my party. Then you're going to print my party and oops, it turns out that two more people showed up. So update my party and print it again. Go ahead and get started. All right. Hopefully that went fantastically. Let me show you the solution. The last time I ate at a restaurant, I think I was with three people. So, I'll create a variable called my party and I'll assign it to three. Then I'll go ahead and print my party. If we run this, we get the number three. I'll go ahead and comment this out so that we don't have too much clutter in our terminal. Now, two more people showed up to the restaurant. So, we'll update this to five. And then when we run this again, our output is five. But let me quickly show you something else. You can reassign variable values elsewhere in your code. So I could come down here and say my party equals 10. And I can print my party again. Now when we run, we see that at first my party is five, but now it's 10. And the thing is is that now my party equals 5 is completely gone because it's been reassigned to the number 10. Much more on that later, but just to show you that that's something that you can do. You've got the basic mechanics of variables down. Store a value, recall it, update it. Nice work. Now that we know how to store values, let's do something with them. I'm going to create a variable called meal total, which I'll assign to the value of 85, and also a tip, which I'll assign to the value of 15. Now I can add these variables directly in a print statement. Meal total plus tip or I can save the result of meal total plus tip to a new variable like grand total and I can print that instead. Let's see what we got. In both cases, Python has evaluated the values stored in the variables and printed the results of adding them together. This is where variables really start to get useful. I'm going to go ahead and delete this print statement because we only need one. So I can show you that if the meal total changes, I can update it in one place and everything that depends on it will update automatically. Now the grand total is 115 without me touching anything else. In our payup application, we'll have a few values that all feed into a final calculation. So this is exactly how that's going to work. Before I give you a challenge, a couple of quick notes on variable naming. Python variables should be lowercase with words separated by underscore. So drink total as opposed to what you might see in a language like Java drink total or something you might see in JavaScript which is camelc case drink total. So in Python we use snake case which are words separated by underscores. The second thing is that Python variables should be descriptive. If drink total equals 5 that tells you something that means something. Looking at our above example, if I said that x equals 100 and y equals 15, that means very little to anybody who's reading your code and your future self. You might also see in Python documentation and in tutorials using the variables fu and bar. I would strongly encourage you to avoid this as well because again, it doesn't really tell you what the purpose of this assignment is. And lastly, variables can't be a word that Python has already claimed. These are words like print, if, for, assert. These are words that already mean something specific in Python and therefore can't be used as variable names. You'll know that you hit a reserved word because Python will throw you an error. So now it's time for the challenge. Step one, you and your friends have just had dinner. Save the cost of the food to a variable called food total. Step two, save the cost of the drinks to a separate variable called drinks total. Three, add them together and save the result to a variable called meal total. Then you're going to print meal total and oops, one friend didn't drink. Reduce drinks total by whatever amount makes sense and print meal total again. And then you'll run your program and make sure that it updated. Go ahead and give this a try. So, first I'll create a variable called food total. Let's say we had a fancy dinner. it cost 150. Then I'll make a variable called drinks total which we'll set to 50. I'll make a third variable called meal total. And we want meal total to be the result of food total plus drink total. And then we will go ahead and print meal total. Before we do the last step, let's open up the terminal, clear it out, and run this program again. And we got 200, which is indeed 150 + 50. So now we're going to reduce the drink total. Let's reduce it to 40. And now we'll run the file again. And our grand total is updated to 190. To build a bill splitting app, we'll need to save different types of data to variables. We'll need to save numbers like the cost of the bill and how many people are splitting it. But we'll also need to save text like the names of the people who are splitting the bill. the name of the restaurant where they ate and things like that. The way to save text to a variable is by using a string. I'm going to create a variable called breakfast and assign it the string pancakes. A string can contain pretty much any character, letters, symbols, even emojis or blank spaces as long as these characters remember are enclosed in quotes. And also remember that you can use single or double quotes. But the main thing is that you need to stay consistent. So I'm going to create a variable called lunch. And let's see what happens if we don't use quotes consistently. I will put salad starting with a double quote and ending with a single quote. When I run the file, you'll see that we get a syntax error. A syntax error generally means that there's something wrong with the way that the Python has been typed. So you can see it says got unexpected string at line 2. And it's also very helpfully pointing out that something is wrong here. So I'll change that back to a double quote. Run it again. And we aren't using any print statements. So, the Python isn't returning anything. We'll fix that in a second. But first, I'm going to create a third variable called dinner, which I will assign to the string pizza. Let's see what happens if I were to assign the value pizza with no quotes at all. This time, I get a name error. Name pizza is not defined. We're getting this error because Python thinks that pizza is a variable instead of a string because we didn't use quotes. So, quotes are how we let Python know, hey, this is some text. It's not a variable or any other type of data. So, let's put quotes around pizza and make sure that everything is running as expected. All right, we've got no errors. So, that's great. I also want to show you that you can add strings together just like you can add numbers together. And this is called concatenating strings. So, in a variable called meal plan, I'm going to assign breakfast plus lunch plus dinner. And I'm going to print the meal plan variable. I'll clear this out and let's see what happens now if we run the file. As you can see, Python has done exactly what we've asked. It's concatenated all the strings together, but it doesn't look quite how we would want it to because it's not very readable with all the words sort of squished together like that. So, there are two ways that we can fix this. The first is that we could add strings to meal plan to create spaces. So here I'm actually concatenating a space in between breakfast and lunch. And I can also do that between lunch and dinner. Let's see what this looks like. So now this is much more readable. We can see that our meal plan is pancake, salad, and pizza. This works, but it's a little bit long and messy. So the other thing that we can do, especially for a small program like this, is that we can just add the spaces to our strings. So, I'm going to add a space after pancakes and after salad, but not for pizza because that's the end of the line, so we don't need it there. Now, let's take a look at what this looks like. You can see the result is the same. Also, here in meal plan, I could add a whole new string. So, to the beginning, I'm going to add a string that says today I will eat colon plus breakfast, lunch, and dinner. And as you can see, that's going to print it out all as one string. Are you ready for a challenge? Here it is. Your challenge is to create a daily planner. A simple daily planner because it's going to print a sentence that outlines your plan for today. You're going to start by making three variables, morning, afternoon, and evening. To each variable, I want you to add a string containing one activity, and that can be anything you want. Taking a nap, climbing a mountain, skiing, studying Python, whatever you want to do for today. Then I want you to add all the variables together and save to another variable called plan for today. Here's an example of what I want your final string to look like. My plan for today is number one, first activity, number two, second activity, and number three, third activity. And here's a hint, there's no magic to numbering this list. Just go ahead and include the numbers in your string. Go ahead and get started. Hopefully that went really well and your day is going to be really exciting or relaxing depending on what moves you. I'll get started by creating my three variables. In the morning I want to drink some coffee. I'm going to make sure to include a space at the end of my string for when I concatenate. And then I will create another variable called afternoon. And in the afternoon I want to study Python followed by a space. And in the evening I want to have a dance party. And this is the end of the list. So I won't add a space. Now it's time to put my strings together. And I will start by creating a variable called plan for today. I'm going to start with a string. My plan for today is colon plus morning plus afternoon plus evening. Now I will print my plan for today to make sure everything looks okay. Open up my terminal, clear everything out, and run my program once again. All right, this almost looks good. It looks like I just need to make sure I have a space between the colon and the one here. So I will go ahead and add that. Add a space right here. Save. And let's run it again. And now it looks good. Great job so far. Hopefully, writing Python is starting to feel a little more comfortable as you get used to the syntax and building up that muscle memory. Let me introduce you to something that's going to make formatting strings a lot easier. They're called fstrings, and they let you drop variables directly into a string as a placeholder. Here's what it looks like. I'll create a variable called about me and in it I'm going to write a string with curly brackets where I want Python to insert values. So I'll say my name is name in curly brackets, my favorite color is color and my favorite place to eat is restaurant. Now I'll define the variables and convention is to put them at the top of the file. I'll set name to treasure, color to blue, and my favorite restaurant is called bow tie. Now, here's the important part. In order to make this an F string, I'm going to put an F in front of the string. And the F, by the way, stands for format. Let's print about me and see what happens. Python found each variable, pulled its value, and dropped it into the string where the curly brackets were. Without the f, Python will print the string exactly how it looks here with the curly brackets and everything, as long as I save the file first. There you go. Let's go ahead and put the f back. And here is your challenge. For your challenge, you're going to create an excuse generator. Say a friend invites you out and you don't want to go. You're going to write a program that generates excuses for why you have to stay in. Step one is to define these variables. First name, event, number, noun, and verb. And you can use whatever values you want to define these variables as long as they match the format. So have fun and feel free to be creative with this because your next step is to write an fstring using your variables that generates a one-s sentence excuse. And I've included an example of what your excuse should look like. This one says, "Sorry, Ted, I can't go to the movies. I have 345 B's to crochet." And honestly, it's taking a little longer than expected. The brackets in the sentence are where you're going to want to drop in your variables. And the idea here is to be able to quickly change the sentence to create whatever wacky excuses you want. So the third step is to print it. After you've printed your first excuse, I want you to change some or all of the values to create a brand new excuse. And then go ahead and print again. Go ahead and get started. Hopefully that went really well and you've generated some good excuses. Let me show you what I did. I'm going to start by creating my sentence, which I'll save to a variable called excuse. make an F string and then I'll say sorry name. I can't go to event. I have number noun to verb and honestly it's taking longer than expected. Now I'll define my variables. I have first name say gym. The event I'll say is the concert. Enter a big random number. For noun I'll say saucepans. And for a verb, I'll say scrub. Now I'll print my excuse. Let me go ahead and comment out my code at the top of the file. And we'll clear this and run again. Looks like I have a little mistake here. This should be first name. Let's try this again. Sorry, Jim. I can't go to the concert. I have 3,432 sauce pans to scrub. And honestly, it's taking longer than expected. All right. Now, let me try changing some of the variables. Nice work on this one. Keep going. Amazing work so far and a big congratulations is in order because you've now learned enough Python to start building the payup app. And we're going to start by building the display, also known as what your users will see when they're done running the program. Before writing any code, let's look at the example output, which I've included in a file right here. This is what your finished output should look like. We have a little introduction to the program. Then you're going to say, here's the breakdown for whatever event or occasion they're wanting to split an expense for. Then we're going to display the cost, the service charges, the group size, the grand total, and finally, what each person will owe. I also want you to notice that for readability, we have some empty lines in the display. You may remember that we can create space with empty print calls and this makes it look a lot nicer and as I said, more readable for the user, which is very important. Now that you know what you're going to build, let's jump straight into the challenge. Your challenge is to create the output display for the payup app. And step one is to check out the example, which we just did, but be sure to reference it as you're building. Step two is to define a variable for each item. We need a variable for the event name, cost, service charge, group size, grand total, and total per person. And I want you to go ahead and use the madeup values in the example output for now. We're going to do the math in later scripts. Your third step is to build the display line by line using print statements and franks. And finally, I want you to run it and make sure that it matches the example. Go ahead and get started on the challenge. I know you can do it. Hopefully that went really smoothly for you. Let's get into the solution. I'll start by defining my variables. I'll make a variable called event. The event is dinner at Fantastic Pizza, cost, service charge, group size, brand total, and total per person. Now that I've defined my variables, let's take a look at the example and see what I need to do. So, I'll start with the first four lines. I'll need an introduction, a space, the sentence where I'm going to drop my event, followed by another space, add my space, the f string where I'll drop my event variable, followed by another space. I'm going to pause here and run my program just to make sure that I haven't made any mistakes so far. This looks pretty good so far, but if I compare it to my example output, you can see that I forgot the colon at the end of the line. So, I'll go ahead and add that. Okay, let's keep going. Now, I'm going to need the next four lines, which are all fstrings, where I'll drop my next four variables. So, I have cost, service charge, group size, and grand total. Put my service charge here. Then I have group size, and grand total. This is a really good example of why it's good to stop and check your work as you go. because I've already noticed that I forgot to put the f in front of the strings to make them fstrings for every single one of these variables. So, let's check this and see. And as you can see, it's just printing my variables. So, I'll go back and turn these all into fings. Now, let's try this again. So far, this is looking like the example. So, that's great. Let's look at the final lines. The final lines, it looks like, are a space and then each person must pay up. And this is where we're going to drop our total per person variable. So let's go ahead and finish up. This line should say each person must pay up total per person. This should be done for now. Let's clear out the terminal and give this a final check. All right, this is looking good. We've got the display done for our payup app. But there's a lot more to come. So keep going and fantastic work. So far, every value in our program has been hardcoded, meaning it's fixed in the code itself and only changes if you go in and manually edit it. But what if you want your program to work with information you don't have yet? That's where the input statement comes in. Input takes a string, which is a prompt, and waits for the user to type something. So, into input, I'll put the string, what is your name? Let's run this. And you get a prompt, and you can type something in. But notice that there's no space before the cursor. And it doesn't look very good. It looks a little bit cramped. So, this is an easy fix. We're just going to come up here and add a space before the closing quote. Now, let's run this again. And that looks a little better. But you may have noticed that when I enter something, it doesn't seem to go anywhere. Okay? And that's because we haven't done anything with the input yet. We need to print it. So I'm going to surround the input statement with a print statement. And that's going to tell Python that we want to print whatever the user inputs. So let's clear this out and run again. And this time I'll put my name treasure. And it prints out the name treasure. But this isn't super useful on its own. To actually do something with the user input, we need to save it to a variable. So I'm going to remove the print statement from around the input and I'm going to save the input to a variable called name. And then I can print the name and we'll still print my name in the terminal. But the cool part is that whatever the user types is going to get stored in the variable name which I can use anywhere in my program like in an fstring. So in my print statement I can create an string that says nice to meet you name. And when I run the program, looks like we forgot this final quote here. And now when I run a program and enter a name, it will echo back that name to me as a greeting. This has a lot of great uses because it can make programs a lot more dynamic and fun. And hopefully that's what you will discover in your challenge. Notice that we have a file called excuse generator, which looks a lot like the excuse generator from a previous scrim. Here's your challenge. You're going to update the excuse generator to accept user input. Right now, all of the variables are hardcoded, which means they're never going to change unless you type a change manually. So, I want you to replace each variable's value with an input prompt. Then, you're going to print and make sure that your output still looks as expected. Go ahead and give this a try. So for the first input, I could say something like what is the name? I could say who invited you. There are many things I could say here, but what I'm going to go with is who is the excuse for? And I'm remembering to include a space to make it look a little nicer as a prompt. For event, I'll simply say what's the event for number. Maybe you remember doing Mad Libs in school. is kind of like that. So, I'm going to simply say enter a number. For noun, I'll say enter a noun. Or to be a little more specific, let's say to enter a plural noun. And finally, for a verb, I'll say enter a verb. Let's run this program and see what it looks like. Python excuse generator.py, we'll say the excuse is for pair. The event is work. Number is 300 unicorns and the verb will be brush. Sorry pair, I can't go to work. I have 300 unicorns to brush and honestly it's taking longer than expected. Input statements really help your programs become more fun and interactive. Awesome work so far. Now that you know how to gather user input, we can upgrade the payup app. Instead of just hard- coding the information, we're going to get some of it from the user. So, right away, here is your challenge. Your challenge, as I said, is to gather input from the users. We need to get four things from the users. The name of the event or occasion, the cost, the tip or service charge, and how many people are splitting the bill. The first step of the challenge is to replace the hard-coded values for event, cost, service charge, and group size. You're going to replace those hard-coded values with input prompts. You can keep grand total and total per person hard-coded for now because they won't be entered by the user. Then finally, I want you to run the program and make sure that your prompts are clear and the display looks right. Keep in mind that we're not having our app calculate anything yet. So, the numbers aren't going to be correct because these values are going to stay hardcoded. It's going to continue to say that the grand total is 330 and the total per person is 110 no matter how these values change up here, but we'll fix that later. Now, what do I mean by make sure your prompts are clear? What I mean is that your prompts are instructions for the user. So the clearer they are, the more likely you are to get back what you need. For example, if one of my inputs was service charge, this leaves a little bit too much room for interpretation. If this were the prompt, somebody might think that we want them to calculate the service charge themselves and then enter it in. So, they might enter the dollar amount or they could type 20% with a percent symbol or 20% or as a response to this prompt, you could even just say yes, there was a service charge. So, here's a prompt that is more specific and helpful. Was there a tip or a service charge? Enter a whole number. Example, 20 for 20%. Because what we're looking for here is a percentage as a whole number so that we can use it to calculate the tip or service charge based on the cost of the expense that the user wants to split. So keep in mind that specificity matters to make sure you're getting the data you expect from the user. Just something to keep in mind. I'm going to comment this out so you can still reference it. And now go ahead and give this challenge a try. I'll start by deleting all of my hard-coded values. For the event input prompt, I'm going to make my prompt, "What was the event or occasion?" For the cost, I'll put something a little more conversational. Let's say, "How much was it?" One thing worth flagging here is that we do want to be specific to an extent, but for the most part at this stage, we're going to trust the user to enter exactly what we've asked for. So here, instead of entering something like 300, they could enter 300 with a dollar sign or they could even type 300 or something like that. And this would cause the program to break or give you bad output. Later on in the course, we're going to talk about error handling in input validation and all the things that you can do to make sure that you're getting the data you expect. But for now, we're going to keep it simple, be specific where we can, and make the assumption that our user is going to enter exactly what we ask for, the right format, the right data type, nothing unexpected. So, let's move on. For service charge, I'm going to grab the example up here. And finally, for group size, my prompt will be how many people were in your group. Let's run the program and test this out. We'll say the event or occasion was dinner at Al's Diner. It cost $300. There was a 10% service charge. And I see that we're missing a parenthesy here. So, we'll need to fix that. Four people in our group. And the display prints just fine. And everything seems in its place. So, I'll go ahead and add this parenthesy where it's needed. And we'll call that good for now. So, we've got the inputs working and the app is starting to take shape. Amazing work. To continue building the payup app, we're going to have to learn a little bit more math in Python. You've already seen some addition. For example, if I were to print 20 + 10, that would evaluate to 30. The word evaluate in Python refers to how Python will compute an expression down to a single value. To subtract in Python, you can use the minus symbol. So, 20 - 10 would evaluate to 10. You can divide with a backslash. So 20 divided by 10 would evaluate to two. You can multiply using the asterisk. So 20 * 10 would evaluate to 200. Just like with addition, you can perform any of these operations on variables. For example, let's pretend for a second that I have a novelty tie collection and the current number of ties in my collection is 15. Now let's say I went out and bought three more ties. If I wanted to keep track of the total number of ties in my collection, I could create a new variable called total ties and set that to the value of current ties plus new ties. Let's print total ties. Let me quickly comment out these earlier print statements so we can see a little better what we're doing. So, Python finds the value of current ties and new ties, adds them together, and saves that value to the variable total ties. As we can see, total ties is 15 + 3, which is 18. Now, say each new tie costs $5 and I want to know how much I spent. I'll create a new variable called total cost and I'll say 5 times the number of new ties. If I print the value of total cost, it should give me 15. And it does. Now, say I realize that I have just way too many ties in my tie collection and I decide to sell 10 of them. Let's make a variable called remaining ties and that will be my total number of ties minus the 10 that I sold. Let's see how many ties we have left. Our total number of ties is 18. So, we should have eight left. There we go. Eight ties left. Finally, I'm done with ties. I want to start collecting something else. So, I decide to gift what's left evenly among my four best friends. I could calculate how many ties goes to each friend by dividing my remaining ties by four. I'll save that to a variable called ties per friend. And let's print how many ties goes to each friend. Looks like I have, let me fix this typo. I have eight ties. So, two ties go to each friend. Notice that division always returns a decimal in Python, even when the result, as it is here, is a whole number. We'll learn more about decimals in Python later, but just keep that in mind for now. One more thing I want to show you is that you can also do math in fstrings. So, instead of saving the result to a variable first, you can do the calculation right inside the curly brackets. So, I could say something in an F string like I have current ties plus new ties ties in my collection. And right in the fstring, it's going to find the value of current ties and new ties and add them together for us and inject them into the sentence. And the result is I have 18 ties in my collection. This works fine for simple math, but it can get hard to read. So, in general, it's cleaner to do the math ahead of time and then save it to a variable. I'm going to go ahead and comment out these examples so that I can give you your challenge. Your challenge is to practice arithmetic operators, which is just a fancy way to say practicing addition, subtraction, multiplication, and division in Python. Here, I've given you three word problems, and I want you to solve them using the following steps. First, save each value to a variable. I'm going to leave it up to you to decide what to name the variables. Next, I want you to do the calculation and save to another variable. And finally, print the results. Go ahead and get started on this challenge. Okay, let's get started on the first problem. You have 200 songs in your library. You delete 47. Print how many are left? So, I'll save the value 200 to a variable called total songs. Then I'll make another variable called deleted songs and assign that to 47. Then to a variable called remaining songs, total songs minus deleted songs. Now we can print remaining songs and get the number of songs we have left. We're left with 153 songs. The next one says, "A movie ticket cost $12. You're buying for yourself and two friends. Print the total cost." So we have a movie ticket that costs $12. And the number of tickets we need is three. So the total number of tickets that we need would be movie ticket. And maybe a more descriptive variable for this would be movie ticket cost. Movie ticket cost times number of tickets. Now we can print total tickets. And we've calculated that we need $36 to buy three movie tickets. Finally, you have 48 stickers to share equally. among six friends. Print how many stickers each person gets. So we have stickers which we'll set equal to 48. The number of friends we have is six. And so stickers per friend would be stickers divided by friends. Now we can print stickers per friend. And eight stickers go to each friend. Let's talk about something that might trip us up when we try to do math in the payup app. Let's say we have a value cost and a value service charge. Notice I've made both of these values strings. Now I'm going to try to print the sum of these values. We would expect to get the value 330 because 300 + 30 is 330. But instead we get 30,030. And that's because Python didn't add the numbers. It just stuck them together like strings because as I said, they are strings. This is important because that's always going to be the case with user input. Input always returns a string no matter what the user types. So before we can do any math, we need to convert our strings to numbers. Every value in Python has a type and we can check it using Python's built-in type function. So I'm going to print the type of cost. The type function tells us that the type of cost is a string. To convert it to a whole number, we can use a built-in Python function called int. Let's print the type again. Now the value of cost is an integer which is a whole number positive or negative. But the payup app is going to be working with money which means we need decimal numbers. In Python, a decimal number is called a float or a floating point number. We can convert a value to a float using the built-in function float. Now, it says the type is float. And if we print the value, you'll see that it's 300 with a decimal. Now, how do we apply this to an input? Let's make one really quick. I'll make an input called budget. And the prompt will be what is your budget for the project? Let's print budget. And let's also print budgets type. So it's prompting me for my budget. I will say my budget is 450. And it prints my budget and lets me know that this is a string. To convert this number to the float, I can wrap the entire input statement in the float function. Now if we run again and I say my budget is $450, it prints $450.0 and the class is now float. This way, if we try to add it to something, let's say I want to add $100 to my budget, I'm able to add this number properly to another number because I have converted it to the correct type. First, I took the string, I made it into a float, and I can now add it to another number. By wrapping the float around the input, that ensures that whatever the user types gets converted immediately. Now, it's time for a challenge. Your challenge is to build a simple paycheck calculator. So, we're starting with these two hard-coded values. And if we wanted to calculate a paycheck based on an hourly rate and the hours worked, we do something like hourly rate times hours worked. But, as you can probably guess, this isn't going to work because hourly rate and hours worked are strings. So, Python doesn't actually know what to do when we multiply two strings together. So it simply gives us an error. String object cannot be interpreted as an integer. In other words, I don't know what you're trying to do here. So what I want you to do is instead of these hard-coded values, prompt the user for their hourly rate and hours worked. Then I want you to convert the hourly rate to a float and the hours worked to an integer. Type check both of the variables and then multiply them together and print the total pay. Go ahead and give this a try. Hopefully that went well for you. Let's get into the solution. So, I'm going to replace this hard-coded value with an input and my prompt will be what is your hourly rate. For hours worked, my prompt will be how many hours did you work? Now, I'll convert the hourly rate to a float and the hours worked to an integer. Let's go ahead and print the types of both. I'll say my hourly rate is 25 and I worked 88 hours. So now we know that hourly rate is a float and hours worked is an integer which means I can do the next step. I'll create a variable called total pay and total pay will be hourly rate times hours worked and then we'll print total pay. Let's give this a try. Again, I'll say my hourly rate is 25 and I worked 88 hours. And as you can see, we've successfully calculated the paycheck. Really amazing work here. Let's keep going. Here we are back in the payup app where I want to take a look at our input statements. As we've learned, right now, everything is coming in as a string. But let's think about what we're going to do with each of these values. event is just going to be displayed. We don't need to do any math with it. So, it's fine if it stays a string. But cost, service charge, and group size are all going to be used in calculations. What we ultimately need to do is calculate the service charge based on the total cost. So, for example, if we get an input saying there's a 10% service charge and the cost is $100, then we'll want to add that 10% to the $100 to get our grand total. Once we get our grand total, we're going to want to divide it by the group size to get the total per person. Don't worry about remembering all that now because we're going to work through it. Right now, what we need to worry about is that if we leave them as strings, we're going to have problems doing math with them. So, the question is, what type do we need each one to be? What type do we need to convert them to? I'm going to leave that up to you to figure out. But what I will say is that cost needs to be a dollar amount. Service charge is a whole number and group size will also need to be a whole number. So that is going to be your challenge. Your challenge is to apply type conversion to the payup app. I want you to wrap the appropriate input calls in a float or an int depending on what type we're going to need. Then I want you to type check the cost, the service charge, and the group size. And then finally, run the program to make sure everything still works as expected. Go ahead and get started on the challenge. Cost is going to be a dollar amount, so we're going to want to convert that to a float. Service charge is a whole number, so we'll convert that to an int. And finally, group size is also a whole number. So we'll convert it to an int. Now let's move on to part two. Type check cost, service charge, and group size. So we can clearly see the types. I'm going to comment out all of these print statements for now. And we'll print the type of cost, the type of service charge, and the type of group size. Say the event or occasion was Sunday brunch. Let's make it really expensive just for fun. Say there was a 20% tip and there were six people in the group. As you can see, we've ended up with the cost as a float and the service charge and group size as both integers. So that's exactly what we want. Let's uncomment the rest of our program so that we can give it a final check and make sure that it's working as we expect. So far, our display looks as we'd expect. I do want to point out that the service charge right now says $20 because we're not calculating based on the cost yet, but that's totally fine. We'll fix that later. But for right now, the program is running and we have the types we need to successfully do the calculations. Great work. At this point, you've learned all the Python you need to update the payup app. So, it does all the work of calculating the total cost and splitting it between the group. There are three things to calculate. The grand total, the total per person, and a new variable I'm going to add right here called service charge total. Here's your challenge. First, you're going to calculate the service charge total. From your prompt here, the user is going to enter a whole number percentage. So, you'll need to convert that percentage to a decimal and multiply it by cost to get the dollar amount. And then you're going to save the result to service charge total. If you're unsure about the math or simply not a math person, you can go ahead and check out the hints MD file for a walkthrough on how to do the calculation. For step number two, add cost and service charge total to get the grand total. And you'll save that to the grand total variable. Your third and final step is to divide grand total by group size to get the total per person. To help things be a little clearer, I'll move the variables. Step one is calculating the service charge total. Step two is calculating the grand total. And step three is calculating the total per person. Go ahead and give this a try. Service charge is a potentially tricky thing here because the user is going to enter a whole number percentage. So as an input, you might get something like 15 for 15%. To get that actual dollar amount, you need to convert that percentage to a decimal and multiply it by the cost. So to convert 15 to a decimal, you divide it by 100, which would give you 0.15. I would then want to multiply the decimal by the cost. So let's say that they entered an expense of $200. I would do.15 * 200. In other words, 15% of 200 is 30. To translate this into Python, I would take the cost that the user entered and I would multiply that by the service charge divided by 100. So again, we're dividing the percentage whole number by 100. So to review quickly, we're converting the whole number percentage to a decimal by dividing it by 100 and then multiplying it by the total cost. And that gives us the dollar amount of the service charge or tip. Now that we have that, we can calculate the grand total, which would be the cost plus the service charge total. To use an example of real numbers, again, if the user inputs a cost of $100 and a service charge of 10%, 10% of 100 is 10. So, we're adding 100 + 10 to get the grand total, $110. Now that we have a grand total, we can divide that by the group size to get our total per person. Now, let's save and make sure it works. We'll say we went to a karaoke night. It cost $200. There was a 15% tip and there were three people in the group. Looks like we have a couple of adjustments here to make. First, I'm going to get rid of our type checking here at the bottom of the file so that's not cluttering up our terminal. And secondly, you'll notice that the math here doesn't seem to add up. We have a cost of $200. Service charges are 15. And yet it says the grand total is $230. And that is because here in our display, we still have service charge in our fing. But we've used service charge to calculate a new variable, service charge total. So if we change that in the display, our math should be correct. Let's clear everything out and try it again. The event or occasion is karaoke. Again, we'll say $200. The 15% service charge, there are three people in the group. So, now we have a cost of $200 with $30 in service charges, which at 15% is correct, a group size of three, a grand total of $230, which does add up, and each person must pay up approximately $76. If you're thinking that a number followed by many decimal points is not an ideal way to display money in this app, you're exactly correct. So, join me in the next one and we'll fix it. Depending on the numbers that the user plugs into PayUP, you might have seen a number that looks something like this. A whole number with a long string of decimals. Because we're dealing with money, we only need to show two decimal places. And luckily, we can fix that with a small addition to our f strings. Let me show you an example. Let's say I've made some kind of budget calculator, and it says the money I have left over from paying my bills is some number with many decimal places after it. I'll print an F string that says, "This month, I have funds remaining after I pay all of my bills." Inside the curly braces, right after the variable name, I'll add colon.2f. The f tells Python that we're formatting a float. And the 2 tells it to display exactly two decimal places. So let's take a look. And you'll see that it's formatted it to display only the first two decimal places. And because the third decimal place is an 8, it has rounded that up for us. And we've ended up with $230.84. So that's how you can format a float in an F string. Here in the payup app, I will give you a challenge. Your challenge is to clean up the payoff display by adding the float formatting to all monetary values in the print statements. So go ahead and do that now. Okay, let's take a closer look at our display and see which numbers could use some formatting. Event is a string, so we don't need it for that. Cost is a monetary value where this formatting would be helpful. So I'll add it here. We'll also want to add this to the service charge total. Group size is a whole number. So we don't need it there. Definitely need it for the grand total as well as for the total per person. All right, let's check this out. We'll say the amount was 200 with a tip of 15% and there were three people in the group. Last time we entered these values, we got 76 followed by many decimal points. And as you can see, that's now nicely formatted for us. Let's run this again with long decimal numbers for every value. The event was karaoke night. We'll say it was 200 followed by many decimal points. Service charge of 10%. And there were five people in the group. And as you can see, it's keeping all of our monetary values down to two decimal points, which looks a lot nicer. Only a little bit more left to go now. Let's keep at it. Before we wrap up, let's add a couple of finishing touches to the app. First, let's move the line welcome to pay up to the top of the file so it's the first thing users see when they run the app. Instead of having it appear after the user inputs everything, a quick greeting is going to set the scene and make for a better user experience. It's going to let people know what app they're using before we start asking them a bunch of questions. Secondly, if this were a production app, meaning an app in the wild that's being used by a lot of people every day, it would need to handle all kinds of unexpected output. For example, when answering these questions, a user might leave a field blank or type the word 20 instead of the number 20. Stuff like this is going to cause unexpected results or even break our app altogether. Handling unexpected input is a little bit beyond where we are right now, but there's one small thing we can do to make our app more forgiving. First, let me show you the problem that we could run into. If the user were to type 20% here instead of 20, our program will crash because Python can't convert the string 20% to an integer. We've asked the user to enter a whole number here, but that may not stop them from entering 20% and breaking the app. We can fix that with the string method called strip, which removes characters from the edges of a string. Let me show you an example. I'm going to comment out the app for now and make a little room here and I'll make a variable called percentage which I'll set to the string value of 30%. Then at the end of this string I'll use the strip method to say I want to strip the percent symbol out of this string. Now I'll print percentage so you can see the percent symbol has been removed or stripped out of the string. Let's apply the same idea to pay up. What we want to do is say that whatever this input ends up being, if there's a percentage sign, we want to strip it out. So at the end of input, I can say strip out the percent symbol if there is one. So remember that user input always comes in as a string. By adding strip to the end, we're telling Python to take what the user inputs and strip the percent sign out of it. And this allows the program to continue as normal. So the percent sign is stripped away, leaving us with 20 as a string, which is then converted to an int and saved as the integer 20 to service charge. Let's take a final look at our finished app with these changes. For the service charge, we'll add 30 with a percent sign. Say there were six people in the group. And there you go. Our app no longer crashes if somebody mistakenly adds that percent sign. This is just a little teaser for all the fun things that you can do with string methods, which we'll dig into properly later on in the course. For now, congratulations. You finished your very first fully functioning Python app. That really is a huge accomplishment. So, I hope that you pat yourself on the back and feel so so proud. A huge congratulation because you have just built your first robust Python application. The PayUP app takes real input, does real math, and displays a clean, readable result. And you've learned a lot along the way. We've covered variables, strings, frings, user input, data types, type conversion, arithmetic operators, and string formatting. These are crucial and foundational Python skills. This course will be released module by module and module 2 is already in the works. So hang tight. We have a lot more good stuff coming for you in the near future. I'm so glad you are here to learn Python with me. Happy coding. Hello and welcome. I hope you're ready to learn more Python. In this section, we're going to build a word scramble game called Jargon Jumble. Let's take a look at the final project. I'm going to go ahead and run Python Jargon Jumble.py. For each round, we're going to show the player a scrambled word that they will have to unscramble. They'll be able to ask for a hint. They'll be able to skip the word, and they'll also be able to completely quit out of the game. So, let's go ahead and ask for a hint. The hint is, of course, we'll hit the blank. No problem. Well, within a couple of days, maybe a week. So, I think that this is the word deadline. After I guess the word, the game will tell me how I did. And I got the word right. So, it confirms that and moves me to the second round. Let's type a wrong answer and see what it does. It tells me that I was incorrect, but does give me the correct answer. If I type skip, it moves me to the next round, but also tells me what the correct answer was. And finally, if I type quit, the game tells me my final score, gives me a bit of commentary, and exits the game. Throughout this part of the course, we're going to build this game from scratch. And I think the really fun part about this game is that you'll be able to make it completely your own by swapping in your own word bank and doing your own theming. My word bank is themed around tech and coding jargon, but you'll be able to use whatever hints and words that you want. As we build Jargon Jumble, we're going to learn about lists. Lists are a Python data type used to store and work with collections of data. We're also going to learn about importing modules, which means bringing in pre-written code to extend what Python can do. We'll learn about ifels blocks to make decisions in our code based on conditions. We'll learn about while loops to repeat code automatically. We'll learn more about string methods to transform and clean up text. We'll also learn about a Python data type called tpples. Tpples pair related data so that they don't go out of sync. And just like last time, we'll build the project in stages with lots of opportunities to practice along the way. Are you ready? Let's get into it. The first thing we'll need for a word scramble game is the list of words to scramble. So, let's start making a list of words. Let's see. I'll make a variable called word one. And to it, I'll save the value apple. And then I'll make a variable called word two. And I'll save the value orange. And then I'll make a variable word three. And I'll save the word banana. This seems like it could get a little hard to manage. Okay. I did this to show you that trying to store a single value in a variable often isn't going to cut it. Sometimes you need to store many values in a single variable. In this case, we need a long list of words to scramble. So, trying to use a separate variable for every word is going to get hard to maintain pretty fast. Luckily, Python has a data type called list, which lets you store multiple values in one place. So, I'll make a variable called fruits, and I'll set its value to an opening and closing bracket. This is an empty list, and I can fill it with multiple strings separated by commas. So a list is comma-epparated items in between an opening bracket and a closing bracket. Lists can hold any data type. As we've seen, they can hold strings, but they can also hold numbers. I can make a list of floats. And I can even make a list of lists. This is a list containing three different lists. And just for fun, I could even make a list of emojis because emojis are just strings. Let's see what a list looks like if I print it. Python prints it out exactly as it appears in the file. We'll learn later how to print list items in a nicer looking way, but now it's your turn to make some lists. Here is your challenge. Your challenge is to create some list data to populate your personal portfolio site. First, create a list called skills with three skills you have or three skills you want to learn. Second, create a list called years with how many years you've spent on each skill. Then, create a list called about me with at least three emojis that say something about your personality or hobbies. You can open an emoji picker to browse and insert emojis using the keyboard shortcuts. So, control, command, space on a Mac. And for Windows, Windows plus period. Finally, go ahead and print all three lists. Go ahead and get started on the challenge. Hopefully, that went well. Let's start making some lists. The first is a list of three skills. Then the number of years. And for the about me, I'll add a microphone because I like karaoke, a computer because I like programming, and a palm tree because I like to travel. Now, let's go ahead and print the three lists. And everything looks good. Lists let you keep a whole collection of related values together in a single variable instead of having to juggle a separate variable for each value. You're doing fantastic work. Let's continue to learn about how to work with lists. See you in the next one. We've learned how we can use a list to store multiple items in the same variable, but we need a way to get an item out of the list so that we can do something with it. For example, we'll need to be able to get a word out of the list so we can scramble it and display it to whoever is playing our game. Every item in a list has a position called index. And you can use that index to access exactly the item that you want. So let's say we wanted to access the first item in this playlist. I can do that with the variable name plus brackets. And inside the brackets, I want to include the list position of the item that I'm trying to get out of the list. In this case, the first item in the list is you might expect it to be one, but it's actually zero. This is basically saying give me the item at position zero in the list called playlist. Now why is this in the zeroth place instead of the first place? Well, that's because Python and many other programming languages start counting from zero. This is called zero indexing and it can feel a little weird to get used to at first, but after you have some exposure to it, I promise it will start to feel more natural. So, I'll go ahead and print this and you'll see that gives me the first item in the list. So let's separately print each item in the list. And you'll see that gives me every item in the list. There's also a shortcut for when you want to grab the last item in the list. So in a short list like this, it's easy to go, I want the last item in the list. So this is item zero. This is item one. This is item two. And then get the item out like this. But imagine if you had a list of several hundred items or a list whose length is constantly changing. then it might not be so easy to get the last item in the list. So to get the last one, you can access it at the index of -1. So an index of negative 1 will always give you the last item in the list. -2 will give you the second to last item in the list and kind of on and on like that. It's basically a way to go through a list backwards. So what happens if you try to access an index that doesn't exist? For example, our list only goes up to 012. So, what happens if we were to try to access an item at index 10? That gives us an index error index out of range. This is Python telling you that there's nothing at that position. This is a pretty common error when you're working with lists. So, now you know what to look for. You can also save index items to a variable and then do something with them like use them in an F string just like any other value. I'll make a variable called top song and to it I will save playlist zero. This is getting the item at the zero index of playlist. So now I can use this variable in an fstring. Say something like now playing top song. And that'll give me a string with a value from the list. And note that I could pull this value out of the list right in the fsting without saving it to a variable first if I wanted to. And that yields the same result. Now that you've learned a little bit more about how to work with lists, here is your challenge. Your challenge is to build a help desk feature that shows who's waiting in line for support. I want you to use list indexing to create a status display that looks like this. It's going to say now helping Ada, next in line is Grace, and just added is Allen. You're going to build it based off of this list. And now helping will be the first person in line. Next in line is the second person in line. And just added is the last person in the queue. Go ahead and give this challenge a try. All right, hopefully that went well. Let me show you what I came up with. Going to start by creating some variables and saving the names I need. So I'll make one called now helping. And this should be the first person in line. So I need the person at the zero index. The list is called tickets. So I will say tickets at zero index. Now I need the next person in line. So I'll make a variable called next in line and to that I will save the second person in the queue. Now I need the last person in the queue who is the person who is most recently added. So I'll make a variable called just added and I'm going to use negative indexing to get the last person in the queue. Now that I have the people saved, I can create my fstrings. So I'll say now helping is the now helping variable. Next in line is the person saved in the next in line variable and just added is the person saved in the just added variable. Let's run this and make sure it works. And that is looking exactly as expected. Fantastic work. To scramble a word, we're going to need to do three things. First, break the word into individual letters using the list function. Then, we'll need to shuffle those letters around. Then, finally, join the letters back into a string. Just like with list, you can access individual letters in a string by index. So, let's say I have the string listen. I can use an index to access any letter in the string. So, let's say I want to get the T. That's 0 1 2 3. So, index 3 is the T. I'll go ahead and print this. and you'll see it gives me back the T in listen. While we can access individual characters this way, unfortunately in Python, there isn't a way to rearrange individual letters inside of a string. To be able to shuffle the letters, we need to convert the string to a list. You've already learned a few built-in functions in Python that convert data from one form to another. For example, int changes a string into a number and float changes numbers into floating decimal points. The function list works in a similar way. We can pass it a string and it will break the string into individual characters. To list, I will pass the string listen. Then I'll go ahead and save this to a variable and then print the variable. And that gives me a list of all of the individual letters in listen. We've converted the string listen into a list. So now we can work with it just like a list. For example, we can use a variety of list functions on it. Like for example, the sorted function. Sorted rearranges items in a list so that they're in alphabetical order. So we're converting the word listen into a list. We're saving it to the variable letters and then we're passing this letters list to the sorted function. Now I'm going to wrap this in a print statement so that we can print the result. And as you can see, the sorted function has sorted all the letters in a list so that they're in alphabetical order. This is actually the first step in solving a really common programming problem, which is figuring out if two words are anagrams. When two words are anagrams of each other, it means that they have the exact same letters only rearranged. So, for example, the word listen and the word silent are anagrams of each other. And we can verify that by splitting both words into a list and then arranging the letters alphabetically. To test this, I'm going to change this variable name to first word. And then I'll create a variable called second word where I will convert the word silent to a list. Then I will sort first word and print it. And I will also print a sorted version of second word. Let's run and see the result. We can see at a glance that both of these lists contain the exact same letters. And so we know that they are anagrams. A little bit later we'll talk about how to randomize the order of the letters and stick them back together into a string. But first, here is your challenge. Your challenge is to write an anagram check. So, for each pair below, I want you to split both words into lists, sort them, and print the results. And you'll know that if the two lists match, then they're anagrams. And if they don't match, they are not anagrams. Go ahead and give this a try. Let's check for anagrams. I'm going to start by converting my words to lists. And this time for variable names, I'm just going to go with the name of the letter we're checking. So the first one is earth. And I'm going to convert earth to a list. And we're comparing that to the word heart. And now I will print a sorted version of each. Let's check this before we move on. Let me go ahead and comment out the example print statements so that we can see clearly what we're doing. Let's try this again. We have two identical lists. So we know that earth and heart are anagrams. Now let's do below and elbow. Again we have two identical lists. So we know we have anagrams. And finally let's do night and tight. This time you should see that we do actually have a difference in the two lists. We have two letters that don't match up. So we know night and tight are not anagrams. Excellent work. Let's keep going. We've seen a few of Python's built-in functions that work like shortcuts for common tasks that we might otherwise need to do ourselves. Functions like print, list, or int. These are functions that are automatically available, meaning they're just sort of always there for you to use. But there is also a lot of built-in helper code that you can import. The helper code that you can import are called modules. And a module is a file containing Python code that you can import and use in your own programs. To use a module, you bring in the code using the import keyword. By convention, it goes at the top of the file. That way, all the code you write afterwards has access to it. So, I'm going to import the random module. Once the module is imported, you can reach its functions with dot notation. So, let's say I can't decide which of these lunch spots I want to eat at today. And I want Python to choose for me. I'm going to use a function called random.choice to randomly choose a lunch spot out of this list for me. So, it's the module name, which is random, a dot, and then the function that I want to use. And that's why it's called dot notation. So, what I can do now is pass my list into the random.choice function. And if I print this, I'll see that it hands me back a random spot for lunch. If I run it again, I'll likely get a different random choice. The random module also has a function called random.shuffle, and this takes in a list and rearranges the items into random order. So, say I wanted to show my lunch spots in a different order each time. So, this will shuffle the list. And when I print it, you'll see that it rearranges my lunch spots in a different order each time. There's something a little bit unique about random.shuffle that I want to talk about quickly. Notice that I shuffled the list and was able to print it without saving it to a new variable. First, I just called random.shuffle, passing lunch spots into it on one line. And the list itself, the lunch spot list itself changed. And that's because shuffle does what's called changing the list in place. In other words, it changes the original list directly rather than handing you back a new copy of the list. That makes it different from something like the sorted function. So I'll pass my list to the sorted function and then I'll print it and then I'll go ahead and print lunch spots again. So the sorted function hands us back a sorted copy of the list. So each item is in alphabetical order. But if we print the lunch spots list again, you can see that the original version is intact. And this is very different from how random shuffle works because random shuffle will shuffle the list itself rather than making a copy of the list, shuffling it, and handing you back that copy. Don't sweat the details of this. It's kind of an advanced concept for now, but it is something to keep in mind about random shuffle because it's what we're going to use to scramble letters for our word scramble. And now I'm going to comment out these print statements because it's time for a challenge. Your challenge is that you're building a feature for a board game app that sets up each match. At the start of a game, you need to put the players in a random turn order and also randomly choose one player to deal the cards. So step one will be to shuffle the players into a random turn order and then print the players list. The second step is to randomly choose one player to be the dealer and print the name of who is dealing this match. This is an example of what your output should look like. Go ahead and get started on the challenge. Import random is already included at the top of the file. Otherwise, that's the first thing I would do. Since I already have access to the random function, first thing I'm going to do is shuffle the players list. Then I'll go ahead and print the shuffled list. Remember that random shuffle shuffles this in place. So all I have to do is print the list and it should be shuffled. There we go. There's our list shuffled in random order. Next, I have to randomly choose a dealer of the cards. So I'll start by creating a variable called card dealer. And I'll pick a random choice with random choice passing the players list to the function. Before I do my fing, let me check my work and make sure that I get a random player. There we go. I get a random player each time. So I'll go ahead and make this into an F string. So I have card dealer deals first. And each time I run the file, I get a random card dealer. This is working as expected. Excellent work. Let's keep going. You've learned how to split a word into a list of letters and how to shuffle those letters into a random order so that they're scrambled. The last step is reassembling the list of scrambled letters back into a word. We can do that using the join function. The join function transforms a list into a string. And it's called join because it joins list items with a separator. Let me show you what I mean. The syntax is a little different than what we've seen before because the separator comes first. So I'm going to say I want to join my list together with a comma. So we're first defining a string that we want to join our list with. Then we can say dot join and then to join will pass the list that we want to join. So I'm going to go ahead and pass a list directly to the join function. Like I said, the syntax here feels a little wonky, but you can read this as with a comma, join this list. I'm going to save this to a variable called result, and then we'll print result to see what it looks like. So now our list is a string that says peace, love, happiness. The words look a little jammed together because there are no spaces. So I'm going to add a space after the comma. And as you can see, that looks a little bit better. Notice that the separator here is just a string. So, you can join a list with pretty much any string. For example, we could use a dash here. We could use a word. And just for fun, we could even use an emoji. You can even join with nothing at all. In other words, you can join with a completely empty string. And that's just going to shove all of the elements in a list together. This isn't super for a list of words, but let's say that each item in our list is the letter of a word. The result is that each letter in the list is joined to form a single word. So that's the last piece. After converting a string to a list of letters and shuffling them, join is going to put them back together into a single word. But here is a little bit of practice using the join function. Your challenge is to join various lists into a single string. First, you'll create a URL by joining this list with a forward slash. Then, you'll join this list with a dash to format a date. This list with a space to create a cheer. And finally, you'll join this list of letters into a single string. Go ahead and get started. I'll start by creating a variable called URL. And then with a forward slash I am going to join this list which I'll go ahead and copy and paste. Next I've got a date which I'm going to join with a dash. Thirdly I have a cheer which I will join with a blank space. Then finally I have a word which I want to join with no separator at all. Because this list is already saved to a variable. I can go ahead and pass in the variable. The final step is to print each variable. And let's have a look. We have a formatted URL, a date, a cheer, and a rejoined word. Thanks to all of your amazing work so far, you've got everything you need to build the first part of the Word Scrambler game. So, let's jump right into it. Here's your challenge. First, create a game file called jargonjumble.py. In this new file, I want you to create a list of at least three words and save it to a variable called words. Then you'll use random.choice to pick a word from the list. The fourth step is to use list, random shuffle, and join to scramble the word. And then finally, I want you to print out the scrambled word. You've totally got this. Go ahead and get started on the challenge. Hopefully, that went really well and you have a word scrambler. Let's walk through the solution. Step one, I am going to create a new file called jargon jumble. Then I'll make a list and save it to a variable called words. And I'll add a few items to the list. Next, I need to use random choice to pick a word from the list. So at the top of the file, I'm going to import random. And then to a variable called word, I'll have it pick a random word from the words list. Before we continue, let's print word and make sure this is working. Okay, run this a couple more times and we're getting a random item from the list. Great. The next step is to scramble the word using list, random, shuffle, and join. So, first I'll convert the word to a list and I'll save it to the variable letters. So, to list, I'll pass my randomly chosen word. So, now let's take a look at letters. And I've split my random word into a list. So, now I can go ahead and shuffle the list. I'll do that by using random.shuffle shuffle and passing in my letters list. Now let's print letters one more time. Clear this out. Run again. And we have all the letters in orange shuffled randomly. The last step is to join them back together into a string. So I'm going to get rid of this print statement for now. Then we'll create a new variable called scrambled word. And then I want to join with an empty string my list of letters. The very last step, we are going to print the scrambled word. And I'm going to go ahead and print it in an F string just so it's a little bit easier to see. I'm going to say scrambled. And we'll print the scrambled word. Let's give this a final run through. The randomly picked word was orange. And here is the scrambled version. Let's run and try this again with a different word. Looks like it's going to keep choosing orange. Here we go. Apple scrambled. So that's great. This is working perfectly. Really excellent work here. We've got the first part up and running. Let's keep going. We've completed a crucial part of the word scramble game, which is, of course, to scramble words. Another big component is going to be figuring out whether or not the player's submitted answer is correct. To do that, we'll need to compare the player's answer to the correct answer and ask Python, are these two things the same. And the way we can do that is with equality operators. To demonstrate equality operators, I'm going to use the Python ripple. Ripple stands for read eval print loop. This is an interactive shell where you can execute code one line at a time and see the results immediately without having to write and save a whole file. It's perfect for small and quick testing. So to open the rebel, I'm going to open my terminal and make it a little bigger. Give us some room here. And I'm just going to type Python and hit enter. You'll see this prompt with a group of arrows. And that means that Python is ready for our input. In the ripple, I can do all sorts of things like I can do some basic math. I could save something to a variable and have Python call it later. In the ripple, I can execute pretty much any Python immediately. You can exit the ripple with C. I'm going to go ahead and clear it out and start the ripple again by typing Python. And you can also quit out of it by using the quit function. Now let's look at comparison operators. This is where you compare two values using the double equals sign. And Python gives us back true. Notice that true is not a string. This is a new to us Python data type called a boolean. And a boolean can only have two possible values which is true or false. A boolean is always going to be capitalized in Python. So true has a capital T. And if I were to compare apple to orange, that would give me false with a capital F. If you're using boolean and accidentally use a lowercase, Python is going to think that you're trying to use a variable name and throw an error. So that's definitely something to keep in mind. So this is basically saying, is the string apple equal to the string orange? And it's not. So Python returns false. So the double equal sign compares two values whereas a single equal sign is for assigning a value to a variable. It's common to mix the two up. So just something else to keep in mind. Let's see what happens if I compare lowercase apple to uppercase apple. This returns false because the double equal sign is case sensitive. And this is going to matter later when we're comparing a player's guess with a correct answer. Another equality operator is the not equal operator. So for example, I could ask Python something like this. This is the non equal operator, an exclamation point followed by an equal sign. And this is basically asking is it true that cat is not equal to dog and that is true. So Python returns true. So it's doing the opposite of the double equals. It's checking whether or not two values don't match. The double equals and not equal operators also work with numbers. So I could say is 5 equal to 5 true is 5 not equal to 5 false is 5 equal to 6 false is 5 not equal to 6 and that's true. So those are equality operators. I'm going to type quit to exit the ripple and close the terminal so we can see how this might work in a file. Let's say I have a password and I want to check whether or not a password that a user entered matches the password I have here. So I'm going to create an input. I'm going to call it user password. And I'm going to prompt the user to enter their password. And I'll print a statement comparing whether the password I have equals the user password. And also just to demonstrate, I'm going to print whether or not password does not equal the user password. Let's open the terminal back up and try this out. When I run the file, it will prompt me for a password. And I'm going to type in what I know is the correct password. So we get does password equal the user password. And it does. So that returns true. But when we check if the password does not equal the user password, they do in fact match. So Python returns false. If I run this again and type a password that doesn't match, you'll see the results will flip. So now does password equal user password? No. So that's false. Does password not equal user password? That is true. I'll go ahead and comment out these print statements so I can give you your challenge. Your challenge is to build a promo code checker for an online shop so a shopper can enter a code and find out whether or not it unlocks the flash sale discount. So, step one, you're going to prompt the user to enter a promo code. Step two, print whether the user's input is equal to the promo code and also print whether the user's input is not equal to the promo code. For a user input, I want you to try both flash 50 in all capital letters and flash 50 in lowercase as a prompt. Just so you can notice that because the double equals is case-sensitive, the results are going to flip. And this is the promo code that you're going to be comparing against. So go ahead and give this a try. So I'll start by creating a variable called user code and I'm going to use an input to prompt the user and I'll say enter your promo code. Then I will print a comparison operator that asks is the user code equal to the promo code. And I'll also print an equality operator that checks is user code not equal to the promo code. Let's check this out. All right. So first I need to comment out this user prompt just so we don't get confused. I'm going to control C out of this clear and we'll run it again. So first I'll enter the promo code 50 in all caps. So is the user code equal to the promo code? Yes, that's true because I entered it exactly as we see the promo code. And is user code not equal to promo code? They are equal. So that's false. Now let's run this again and try it with a lowercase code. And you can see that the results have flipped because the equal operator and the not equal operator are case sensitive. Ideally in the real world, your user would be able to enter either of these prompts and they would both work. and we're going to address that later as we move along with our project. But for now, you've done a really great job. Let's keep moving right along. Double equals and not equals check equality between two values. But sometimes you need to know which value is bigger or whether one value is at least as large as another. And this is what comparison operators are useful for. Let's take a look at them. I am going to open up the terminal and start running the Python ripple. Comparison operators are basically symbols that you've probably learned in a math class like greater than, less than, greater than or equal to, or less than or equal to. So we can use them to check for example numbers like is 10 greater than 5. And Python returns the boolean true because 10 is greater than five. What about whether or not three is less than one? That is false because three is bigger than 1. If I were to check if 10 is strictly greater than 10, I would get false. But if I check if 10 is greater than or equal to 10, I would get true because 10 is not greater than 10, but it is equal to 10. As we can see, a comparison operator is always going to evaluate to a boolean value of true or false. So let's say I was building some kind of age checker. My age is 20 and I can have a variable called is adult and I can drop a comparison result into this variable. So I could say age is greater than or equal to 18. Age is greater than or equal to 18 is going to compare age to 18. So is 20 greater than or equal to 18? It is greater than. So this whole statement will ultimately evaluate to true. And you can see that if I were to print is adult. Let's go ahead and check this out. So you can see that the statement with a comparison operator did indeed evaluate to true and that is what's ultimately saved to the is adult variable. You can also drop a comparison straight into an fstring. So I could say I'm going to print the f string is old enough and in my brackets I'm going to put a comparison straight in. So Python will compare age which is 20. See if it's greater than or equal to 18 which it is. So this statement will evaluate to true which will show up in my fing. Let's go ahead and look at this just to make sure. And you can see there is old enough true. Putting a check inside an string like this is handy when you want to label a result. The final thing I'd like to show you is that these operators do actually work on strings. So I'm going to print is banana less than cherry. And Python is going to compare these alphabetically. When I run it, you can see that is banana less than cherry evaluates to true. And that's because Python ends up comparing them alphabetically and banana comes before cherry in the alphabet. This isn't something that you're going to use very often, but I wanted to point it out. I'll go ahead and comment all of this out so I can give you your challenge. Your challenge is going to be a series of comparison checks. Apps often compare numbers to decide what to show a user. So using these values below, I want you to use comparison operators inside fstrings to print a labeled result for each check. And this is what your output should look like. Has read message more than zero should equal false. Old enough to rent a car where the threshold is 25 or older should equal true. Under the $50 free shipping minimum should equal true. and sold out, which is zero or fewer tickets left, should equal false. So, you're going to create fstrings and drop some comparison operators inside so that this ends up being your output. Go ahead and get started on this challenge. Okay, so here is my solution. I'm going to make my fstrings right inside print statements. And I'll start with my string text has unread messages more than zero. So this needs to check whether unread messages is greater than zero. Anything over zero in this case means we have unread messages. But because we have zero unread messages, this is going to evaluate to false as it should. For the second statement, I will start my string. And I know this comparison operator needs to evaluate whether the entered age is 25 or older. So the key here is that we want to return true if the value is 25 or above 25. So I'm going to want to say if age is greater than or equal to 25. Let's check what we have so far. Looks like we have a little typo here. That's why we check. So let's clear and try this again. So the first statement evaluates to false and the second one evaluates to true, which is just what we need. The third one is checking whether a value is under the $50 free shipping minimum. So for this one, we need to check if cart total is less than 50. If cart total is less than 50, no free shipping. The last statement needs to check whether some tickets are sold out or not. And tickets are sold out if there are zero or fewer tickets. So, we'll say sold out zero or fewer tickets left. And we'll want to check if the value of tickets left is less than or equal to zero. Let's check this out. Looks like I have another typo here, but luckily Python is very helpful. It says, "Did you mean tickets left?" And as you can see, I wrote ticket left. So, yes, thank you, Python. Let's try this one more time. All right, we're looking for our statements to evaluate to false, true, true, and false, which they do. So, we know that we have checked our comparisons correctly. Really amazing work, and I'll see you in the next one. Nearly every Python program is going to need to make decisions. For example, in the word scramble game, we're going to want to run certain code if the player guesses the answer correctly and run different code if they guess the answer incorrectly. To make decisions in a Python program, we use if else blocks. An if statement is going to check a condition. So, let's say we have a noise meter app and we want to check whether a room is too loud measured in decb. So, I'm going to create a decibb variable and I'll set it to 90. Now I'll make an if else block that checks the value of decb and does something depending on its value. So for example I can say if decb is greater than 89 I'm going to print the message warning loud environment. An if statement evaluates an expression and in this case it's evaluating whether or not decb is greater than 89. So this statement will ultimately evaluate to the boolean true. So it's the same as saying if it's true that decibb is greater than or equal to 89 print this warning. So if we were to run this you can see that the warning gets printed because the condition that we're checking for is whether or not decb is greater than 89 and decb is 90. So the statement evaluates to true. The expression in this if statement could be checking any number of things like whether a player guessed correctly whether a number falls in a certain range or whether two strings are equal. If this statement were to evaluate to false, so let's say that the decibel level was 20. We'll run this again and you'll see that nothing happens. Nothing is printed. Nothing will happen unless I include an else block that tells Python what to do if the if statement isn't true. So under the if block I can say else will print a different message. Now when we check this out this statement has evaluated to false. So it runs the code in the else block. Keep in mind that the else block is optional and sometimes you'll want to do something when the condition is true and otherwise do nothing at all. I also want to take a second here to talk about indentation because it's a very important thing in Python that we haven't encountered yet. Python requires these print statements to be indented by four spaces and that's how Python knows that this print statement is inside of the if block. The Scrimpa editor and most text editors will do this for you. But this is still a really important thing to keep in mind because indentation errors are very common when you are writing Python. In a programming language like JavaScript, your if statement might look something like this where you would use curly brackets to indicate what is inside of the if statement. So then here you would console.log something. But Python depends on that indentation to know what code is in the if block and what code is in the else block. So again, the Scribba editor will mostly do that for you, but it's something to be aware of. All that being said, now it's your turn. And here is your challenge. For this challenge, you're going to build a results screen for an online course quiz where a student needs at least 60 points to pass. So, first you'll use an input prompt to ask the student for their score and make sure to convert it to an int. If their score is 60 or higher, print a message telling them that they passed. Otherwise, print a nice message telling them that they haven't passed this time. Go ahead and get started on the challenge. All right, I hope that went really well. I am going to get started by making my input prompt. And for that, I will just say, enter your quiz score. I'm going to make sure to convert whatever I get back into an integer. And then if the score I get back is greater than or equal to 60, I'm going to print a message that says, "Congratulations, you passed. Nice work." Else, I'm going to print, "You didn't pass this time. Please try again." Let's run this and make sure it works. I'm going to enter a quiz score of 60. Congratulations, I passed. All right, let's try entering a score less than 60. We'll say 23. We didn't pass this time. Please try again. An if else block gives you two paths. If a condition is true, run this code. And if it's not true, run this other code. But often you'll have multiple conditions you need to check and run code for. So there's another option called L if that lets you chain as many conditions as you need. If you were to write a bunch of if statements, for example, let's say that I have a test score of 95 and I need to check if the test score is greater than or equal to 90. And if so, I'll print that the student got an A. But I also want to check if the test score is greater than or equal to 80 because in that case, they score a B. The problem with two if statements is that both of these lines of code are going to run. So both if blocks will run because both are true. A student can't get both an A grade and a B-grade, but we want to be able to check their scores to assign a grade of A, B, C, D, E, F, or any number of other options. So there's an option called L if that will stop at the first match. So it looks at the score which is 95 which is greater than or equal to 90. So it prints a and then the code stops. LF basically says only check this condition if the previous condition wasn't true. So let's change this to a score of 89 and run it again. Now it's going to look at this first condition. Is the test score greater than or equal to 90? No. So it's going to test is the test score greater or equal to 80? Yes. So it prints B. Now let me keep chaining down through the other grades. If test score is greater than or equal to 70, we'll print a grade of C. Or if the test score is greater than or equal to 60, we'll print a D. And then finally, if none of those conditions are met, we will print an F. So let's say we have a score of 85. Python is going to skip the first condition. It will then evaluate if 85 is greater than or equal to 80, which it is. So, it will print B and then stop. It won't continue on down the rest of the L-s. The else here serves as a catch all. So, it's only going to run if none of the other conditions are true. So, if we have a score of 59, for example, that's going to print an F. So, keep in mind that order matters here. I'm going to change this back to 85. And let's switch these two conditions. So I'm going to check first if the score is greater than or equal to 80. Let's also switch around our print statements and run this and see what happens. If we change the score to something like 95, which is an obvious A. Let's run and see what happens. You'll see that B actually prints because this condition is technically being met. 95 is greater than or equal to 80. and it assigns B because it says yes, this is true and it stops there. It never actually makes it to this condition. So, just keep in mind that the order matters. I'll change this back so it's in the right order and we'll go ahead and comment this out so that you can start your challenge. Here's your challenge. You're building a feature for a coffee shop that has spotty Wi-Fi. The feature should give customers a discount based on how many signal bars they're getting. And for the purposes of the exercise, we're going to say the signal is currently two out of five bars. So, you're going to write an if l if else chain that prints a message informing the customer of their discount. If there are zero bars, you'll print a message giving them 50% off and say, "Sorry about the Wi-Fi." For one or two bars, 25% off. For three or four bars, 10% off. And if there are five bars, you'll inform the customer that we have full bars today and so there is no discount. Go ahead and give this a try. I'm going to start off with if the signal equals zero. If there are no bars, we're going to print 50% off. Sorry about the Wi-Fi. Else if the signal is less than or equal to two bars. So, if there are one or two bars, we'll print 25% off. Else if the signal is less than or equal to four, we'll print 10% off. And finally, if none of these conditions are true, that means that we have full bars and we can print full bars, no discount today. Let's test this out. We have a signal of two. So, we should get a 25% discount. Let me go ahead and save first and we'll try this again. Here we go. 25% off. Let's try changing the signal to five. Full bars, no discount today. Let's change the signal to zero. 50% off. And for a final check, let's do a signal of three. This is less than or equal to four. So, we should end up with a 10% discount. And there you go. Fantastic work. You've already used a couple of string methods like strip, which you've used to strip a percent sign off a value. And then there was also join, which turns a list back into a string. So string methods are built-in tools that transform or work with text. You call them with dot notation. So the string first, then a dot, then the method name. So let's learn about a few more of them, including some more ways that you can use strip. Let's say I'm building a recipe search app. Users will type in what they're craving, but I can't always control how they type it. Someone might search for pasta carbonara in all caps or add extra spaces around their query. String methods will let you clean up that input so that it's consistent no matter what the user types. So for example, let's say I have a recipe named chicken tikka masala. If I wanted to display this in a bold way, I can go ahead and print this. And I am going to call the string method upper on the value of recipe. And you can see what that does. It returns the string in all caps. So this could come in handy for displaying a title or a header. One thing to keep in mind here is that string methods don't change the original string. They return a new one. So if I were to print recipe again, you can see that here when I call upper on recipe, it returns an uppercase version of the string, but that the regular string is still intact. So if I wanted to save this uppercase string and do something with it, I'd need to save it to a variable. And here you can see I have my original string and then the uppercase version of the string which I have saved to header. So the lower string method does the opposite of upper. So let's say I have a search query in my recipe app and the user has searched for hasta carbonara. I'll print the search and I will call the lower method on it and that returns an all lowercase version of the string. This is great for normalizing a search query before matching it against results. There's also the string method title which capitalizes the first letter of every word. So, this can be good for cleaning up names. Let's say I have a chef named Grace Smith, and I want her first and last name to be capitalized. So, I can call the title method on the string, and that will capitalize her first and last name. You've used strip before to remove a specific character, like removing a percent sign from a percentage like this. So, if we print this, it's going to return the string with the percent sign removed. But when you call strip with no argument at all, it removes white space from the beginning and end of a string instead. So let's say I have a recipe or a search query where the user has included a bunch of extra spaces. I can call strip on the string without passing anything into it. You'll see it returns a string with all of the extra space removed. You can also chain methods together, which means calling one right after the other on the same line. So let's say a search query comes into my recipe app and it's a big mess. We'll say raw for raw data. We'll say that there is a bunch of spaces and then you know they type something like this. So we'll make a variable to save the cleaned up version which instead of calling cleaned up, let's just pretend that this is a search query that we're cleaning up. So I can take raw which is this string right here and I can call strip on it to strip out all of the unnecessary space and then I can use another dot and connect this directly as a chain and I can call lower. So this will both strip out all of the extra space and convert this to lowercase. So let's print query and see the result. All the white space is gone and it's been converted to completely lowercase. And that is how you can chain methods together. Now, let me give you your challenge. Your challenge is to do some data cleanup. You're building a signup form that tidies up whatever the user types in. So, each of these values below has come in a little bit messy. Your task is to figure out which string method or multiple string methods will help get the data to its cleanest version. And then you're going to print the result. So, some of these only need one method to clean it up, and some might need two. So, here we have the raw data that is saved a promo code, and this is what the cleaned up version needs to look like. And same with the rest of these on the list. Here's the raw data, and here's what you need to end up with. So, go ahead and get started on this challenge. Give it a try. All right, I hope that went really smoothly for you. Let's get started. So for this first one, I can see that it just needs to be in all caps. So I'm going to print the result of calling upper on promo code. Let's give this a quick check. And there we go. Spring 25 in all caps. Next, I can see that the first and last name needs to be capitalized. So I'm going to use the title method on full name. I am going to call title. Let's give this one a quick check. There we go. That's now in title case. For the next one, I can tell I have some extra space that I'm going to need to strip out. And I can see that the finished result needs to be in all lowercase because it's an email. To email, I'm going to first strip out the white space. And then I am going to chain the lower method, which should get rid of the extra space and convert everything to lowercase. And it does. Finally, for the display name, I can see that I need title case and to strip out some extra space. So to display name, I will first call strip and then title. Let's take a final look. And everything is looking how it should. Congratulations, you've cleaned some data. Cleaning data is a very important and very often used skill for a programmer. You've done great work so far. I hope you're feeling good. We now have the skills to add some interactivity to the word scrambled game. At this stage, we want to be able to prompt the player to either guess or skip the scrambled word. Then, we want our game to respond based on whether the player is right, wrong, or chose to skip. So, here's what you'll need to do. You'll want to add interactivity so a player can either A skip the word and get the correct answer or B guess the word and find out if they're correct. So your first step will be to prompt the player for input and give them two options. The option to type a guess or to type skip to skip the word. As a second step, I want you to use string methods to clean the player's guess and do a minor improvement on the display. We want to be as forgiving as we can with the player's guess. And that means we need to clean the player's guess so that the capitalization and whites space don't matter when comparing the user's input to the correct answer. So in other words, if the correct answer is bandwidth, we want the player to be able to type bandwidth or bandwidth or even, you know, something like this. Basically, as long as they've entered the right word, we want to accept it as a correct answer. The second part of this stage, I want you to make a minor improvement, which is display the scrambled word in all caps so it stands out on screen. The third part is to use if l if else to handle these three cases. So if the player types skip, we want to print the message skipped the word was whatever the correct word was. If the player typed a correct guess, we want to tell them your guess was correct. And finally, if the player's guess is incorrect, we want to give them a nice little message that says, "Sorry, this was actually the word." So, go ahead and work through this step by step, I know you can do it. Okay, let's do the first step, which is to prompt the player with two options to either type a guess or type skip. So, to a variable called guess, I'm going to have an input to prompt the user. And I'll keep it simple. I'll say guess the word or type skip to skip. Now I'll do part one of the second step which is cleaning the player's guess. So the input function is going to prompt the player and when I get that guess back I want to go ahead and strip it for any white space on either end of the guess and I'm going to make it all lowercase and that will effectively make the guess case insensitive. The second part of this stage is to display the scrambled word in all caps so it stands out on screen. So I'm going to come here to where I am joining my scrambled word and I'm going to make sure that it's in all uppercase. Now for the if l if else block. So first I'm going to check if the user has typed skip. So if guess equals skip and I'll make sure it's a double equals. I want to print an f string that says skip and the word. And instead of apple here, I've got the word that was randomly chosen from the list up here. So in curly brackets, I'll go ahead and display whatever word was chosen. Let's check our work so far. So we have a couple of small little problems here. For one, we don't want to print the correct answer. So I'm going to go ahead and get rid of this print statement. And then we've got our capitalized scrambled words. So that's great. Looks like we need to add a colon and a little bit of space here so that this prompt looks nicer. I'll do that now. I'm going to go ahead and control C to get out of this and we'll clear and run it again. And that looks a lot better. Let's see what happens when we type skip. Great. So, we've got our skipped message which is displaying the correct answer. Let's try it one more time just for fun. Looks like we got apple again. Let's see if we can get something besides apple. Okay, here's banana. we skip the word was banana. So fantastic. This is all working great so far. So next, let's check for a correct guess. So I'm going to say else if guess equals word. So we input the player for a guess. And whatever they input, we're going to compare to what we know is the correct answer. If these two are equal, we'll know it's correct. And we can print a message correct. I'm going to go ahead and add a little emoji here. This is totally optional. and make sure that it has adequate space. Let's go ahead and make sure a correct guess is working. So, we got banana. Let's type banana and correct. So, that's fantastic. Finally, we want to handle the case of an incorrect guess. So, here we can go ahead and use an else because unless the player types skip or unless the player types the correct word, we know that their guess is incorrect. So, we can just let this fall to the else block, whatever doesn't match these two conditions. and we'll go ahead and print this message, but we're going to drop word in here like we did for skip. So now we're testing an incorrect answer. So the scrambled word is obviously apple. I'm going to type orange. And we get the message, sorry, the word was word looks like we have a little mistake here. So let's see what is happening. What's happening is we forgot the f for fstring. So let's try this one more time. So the scrambled word is apple. Let's once again type orange. And now we get no. Sorry, the word was apple. I'm going to make sure that there is a period at the end of this sentence. And we can go ahead and call this done. Fantastic work. I do want to note that you can check for skip or check the correct answer in any order here. So it's totally fine if you did it the opposite way. We're on our way to a fun little working game here. So let's keep at it. This scrim is going to cover a Python data type called tpples. For the record, there's no consensus on whether it's pronounced tupil or tpple. I say tpple, but either is fine. Let's say I'm building a design tool and I need to store a color. On the web, colors are often written as a hexadimal code like a hash followed by a series of numbers and letters. But under the hood, it's really three numbers. The amount of red, green, and blue in a color. For example, the RGB values of the color steel, blue are 70, 130, and 180. So, this is the syntax of a tpple. It looks a lot like a list only with parentheses instead of square brackets. And a tpple is perfect for storing values that always belong together, like the red, green, and blue values that make up steel blue. You can get the values out by index. the same as you would a list. So I'm going to print steel blue at index zero and that will give me the value in the tpple at index zero. The big difference between tpples and a list are that tpples are immutable. Once you've created one, you can't change the values inside. So, for example, if I were to try to change the red value to 300, I'll grab steel blue at the index of zero, which is the red value, and I'll try to reassign the variable to 300. Then I'll attempt to print it again, and I get a type error. Tupppel does not support item assignment. So, keep in mind that there is a difference between changing the tpple itself right here. So, I could change this to 300 right here if I wanted. That would be no problem. But when I try to reassign it a different value, that's where the trouble comes in. And that's where Python will say, "No, this is the value. You can't change it." This is Python's way of enforcing the grouping. These three numbers describe one specific color. So, it makes sense that they stay locked together. And that's why you might choose to use a tpple over another data type like a list. With tpples, there's also a nice way to pull out values that's called unpacking. Instead of using indexes, I can unpack the tpple in one line with a series of variables separated by commas. Because there are three values in the tpple, Python will know that red is going to equal 70, green is going to equal 130, and blue is going to equal 180. So the names on the left that we're assigning to these values just need to match the number of items in the tpple. I'll print out the three variables so you can see how they're assigned. You can see that red is now 70, green is 130, and blue is 180. So this is a quick way to assign every value in a tpple to a variable. In practice, you'd often store a whole list of tpples into a color palette list. I'm going to save three tpples representing the RGB values of three colors. These tpples are list items. So to access the first item in the list, for example, I can access it by index. So I'm going to print color palette at the zero index and that'll give me the first tuple in my list. To unpack the first palette in the list, I can unpack exactly how I did up here. So I'm going to unpack the red, green, and blue values, except this time I'm accessing the list and then I'm accessing the item in the zero index of the list. And then I am unpacking each of these values into a variable. To show you, I'm going to print a string that prints out these three values. If I wanted to print out the values from the second item in the list, I could do the same thing. Only this time, I'm going to unpack the tupil at index one of the list. So that'll be this one. Go ahead and copy it and paste this line. And that prints the values of the second tpple in the list. Notice that I am repeating myself using red, green, and blue and copying the same pair of lines for each color. This works, but if we wanted to unpack a lot of tpples, it would get tedious pretty fast. Python has a cleaner way to handle doing something to every item in a list, and we'll get to that later on in the course. But for now, I have a little challenge for you. Your challenge is to build an inventory display feature for a small office supply store. For step one, you'll unpack the item and the quantity for each entry in this list of tpples. In step two, I want you to print each item and its quantity in this format. So, the string will have the item and then the quantity in stock. Go ahead and get started on the challenge. Hopefully, that went really well for you. Let me show you my solution. I'm going to start by unpacking the item and the quantity. And I'm going to grab the tpple that is at index zero in the list. So inventory index zero. And I'll print my f string. And looking at the format here, it's going to be item colon quantity in stock. Let's check this before we move on. Tiny little typo here. Let's try this again. All right. Notebooks 42 in stock. Perfect. Going to come up here and comment this out really quick. Then I'm going to pull out the item at index one. And I'm going to go ahead and copy and paste this print statement. Let's give this a look. There we go. Pens 130 in stock. And finally, I will get the item at index 2 and print out my string. Looks good. That is how you unpack a tpple in a list of tpples. Currently in our jargon jumble game, the players see a jumble of letters that could be really tough to guess without any hints. So, let's give them the option to ask for a hint. I've replaced the small list we've been working with with a list of tpples that pairs a word and a hint. Notice that I've also changed the variable the list of tpples is saved to to wordbank. And wordbank is also what I'm now passing into random choice. We don't want to give the hint away automatically. So, we'll let the player type hint to ask for a hint. And wiring that up is going to be your job in this challenge. So, the word bank below pairs each word with hint. Wire up a hint option so a stuck player can reveal the clue and then keep guessing. Your first step will be to pick a random pair from word bank and unpack it into a word and a hint. Then, you'll want to update the guess prompt so that the player knows hint is an option. If the player types hint, you'll want to show them the hint, then ask them to guess again. So, here are a couple of hints for you. Getting them to ask again will require adding a second input prompt. And your second hint is that you'll want to check if the user has typed hint in a separate if block above this skip, correct, wrong block. And that is because skip, correct, and wrong all end the turn, but a hint doesn't. So, keeping it separate lets the game show the hint and then still be able to check the guess the player types next. And then finally, a little optional challenge here. Add a couple of your own word and hint pairs to the word bank. This is a bigger challenge, but I know you can do it. Take it one step at a time, and I'm sure that you'll do great. Go ahead and get started. All right, step one. Pick a random pair from word bank and unpack it into word and hint. So here where I am choosing a random word from the word bank, I'm going to unpack into word and hint. And the rest of it can stay exactly the same. Let's go ahead and check this by printing word and hint. The I'm going to go ahead and comment out the rest of it for now. So as you can see, we're successfully printing a meeting with its hint. So now I'll get rid of this print statement and bring our code back so that we can move on to step two. Step two is to update the guest prompt so that the player knows hint is now an option. Okay, so here's the guest prompt and I'm going to go ahead and change the prompt a little bit. And I'll say or type hints skip. Let's give this a little bit of space and that should be good. Now moving on to part three. We want to check if the player types hint. And if they type hint, we want to show them the hint and then ask them to guess again. So above my first if else block, I'm going to do a new if statement that says if guess equals hint. In other words, if they've typed hint, I want to go ahead and print the hint. So as part of an fing, we'll say hint. And then we'll print the hint, which remember we're getting from unpacking the random choice from our word bank on this line. Then after we've printed the hint, we want to ask the player to guess again so that we don't interrupt the flow of the game. So I'm going to create another guess prompt in which I'll ask for the guess again or remind them that they can still skip. And I'll make sure to clean and strip this input as I'm doing up here. I think we've done it. Let's go ahead and test this out. So, we have this scrambled word. Let's ask for a hint. The hint is I spent 4 hours trying to blank my code. Turns out I was missing a comma. So, I think the word here is debug. Notice that this is the second prompt that we provided in this guess block. So now when we guess the right answer, it's going to move on to this guess block. It's going to determine that the guess of debug is equal to the word debug and give us the message correct. So I think a little bit of spacing would be nice here just for a final touch. So I'm going to add a blank print statement here. And I'm also going to add one here just to make a little bit of room, make things a little bit more readable. So let me go ahead and ask for a hint. I spent four hours trying to blank my code. All right, looks like we've got debug again and we are correct. I'm noticing that I have a missing quote here. So, I'll also fix that and this is looking good. Great work. Right now in our word scramble game, you can only play one word at a time. To get a new word, you'd have to keep rerunning the program over and over again. So, we want the player to be able to play multiple rounds. And to do that, we're going to need a while loop. So, let me show you a while loop that counts to five. I'm going to start by initializing a variable called count, which I'll set to one. Initializing a variable means setting its starting point before the loop runs. So, for the loop itself, I'm going to say while count is less than or equal to 5, I want to print the value of count. But here's the important part. Every time this loop runs, I am going to increase count by one. And I can do that by reassigning the variable count to the value of count + one. So this loop basically says as long as it's true that the value of count is less than or equal to 5, I'll keep running the code in this block. And this is another instance where indentation is really important. This indentation is what tells Python that this is the code inside the while loop. Let's walk through this so that hopefully it'll be super clear. On the first pass, count equals 1. Is it true that count is less than or equal to 5? Yes. So the code in this block will run. So the loop will print the value of count and then it will increment count by one which would make count two. The loop will then check again is count less than or equal to five. Yes, because count is two. So we'll print count which is two and then we'll increment count by 1. So count is now three. Then once again is count less than or equal to 5? Yes, because three is less than or equal to 5. So it'll print the count and then increment the count by 1. So now count is four. And so on and so on until we reach count six. Then the while loop will ask is 6 less than or equal to 5 and that is false. So the loop will end. Incrementing a variable like this is a very common thing to do. So Python has a shortcut for it which is that we can say count plus equals 1. This is a shorthand way to say increase the count by one. I'm going to initialize count back to one and actually run this so you can see it in action. It prints the value of count until the condition count is less than or equal to 5 is no longer true and then it exits and the result is that it prints out 1 through 5. Something that's really common when writing loops is the accidental infinite loop. So let's say I write a loop but I forget to increment count every time. What's going to happen is that the loop will say is count less than or equal to 5? Yes, count is one. So, it's going to print count and then ask again, is count less than or equal to five? Yes. And it'll just keep going on and on like that forever. And it can eventually crash your browser. I'm not going to demonstrate because I don't want to mess up the terminal or this recording. But just know that you can usually stop an infinite loop by using the keyboard shortcuttrl C. And that kind of forces it to stop and break out of the program. I'm going to go ahead and add my count back. something inside the loop must eventually make this condition false so that it will stop. There's also a way to exit a loop intentionally from inside and that is with a break statement. So I can say something like if count equals 3 then break. The break statement will stop the loop immediately no matter what the condition says. So let's run this and you'll see that when it gets to three it'll automatically break out of the loop. So, as you can see, it prints one and two and then it stops. We're going to use break in our word scramble game to let the player quit midame. So, this while loop pattern is something that you'll end up using a lot. Initializing a variable before the loop, using that variable in a condition, and updating it inside the loop. Let me show you one more example. Let's say that I'm tracking a runner through a race. I'm going to set the total number of laps and track which lap they're on. So, I'm going to set total laps to five. And I'm going to initialize a lap to one. I've put total laps in all caps because that's a Python convention for a constant. A constant is a value you set once and that doesn't change anywhere in your application. So this is in contrast to the lap variable, for example, where we're going to keep incrementing it. So I'm going to say while lap is less than or equal to total laps, I'm going to print which lap we're on. And each time the loop runs, I'm going to increment lap by one. Just for fun, so we can see something a little different. I'm actually going to make this 10 laps. Let's run. And you'll see that it counts the laps until we get to 10 and then it breaks out of the loop. We're going to do something really similar with the word scramble game to make sure that the player can play multiple rounds of words before the game ends. So, here's a chance to practice this pattern. Your challenge is to build an airport app that prints a boarding pass for each passenger on a group booking. So, you're going to want to write a while loop that prints one line per passenger. And that should look like this. Printing boarding pass 1 through 5, printing boarding pass 2 through 5, and so on till you get to the last passenger. So, you have a total of five passengers, and you want to loop through all five passengers starting with passenger number one. Go ahead and give this a try. Okay, so I know I want to go through passengers 1 through 5. So I'm going to write a while loop that says while passenger num is less than or equal the total number of passengers. When I write a while loop, I like to start with the condition that is going to eventually stop the while loop so that I can make sure and prevent an infinite loop. So I'm actually going to start by incrementing the passenger number. So every time this loop runs, we want to increment it by one. So I know that no matter what I do, this number is going to increase by one so that this condition is eventually false so that the loop stops. All right. Now that I've done that, I can print my message. So I'm going to say printing boarding pass and I want to print boarding passes 1 through 5. So I want to print the value of passenger num each time the loop runs along with the total number of passengers. All right, that should be good. Let's give it a look. And we have successfully printed our boarding passes. Super awesome work. Let's continue on. You've just had a chance to work with the counter loop pattern, which uses a constant to determine how long a loop should run and a counter that climbs toward that target number. Let's use this pattern to give the word scramble game multiple rounds. So, here is your challenge. Right now, the game ends after a single word. Add the ability to play multiple rounds and quit out of the game at any point. Step one is to wrap the game code in a loop that runs for five rounds. Step two, display the round number at the beginning of each round. And step three, give the player a way to quit early by typing quit. And here's a hint. You'll want to break out of the loop when they type quit. You've done some really fantastic work so far, and I know you can do it. Go ahead and get started on the challenge. Let me show you how I did it. First, I'll set up the variables for my while loop. So, I'll create a constant called rounds, which I'll set to five, and my counter variable, which I'll set to round number, which I'll initialize to one. I'll create my while loop, which checks for the condition while round num is less than or equal to the constant rounds. Then, I'll highlight all of my code and push it forward so that it's indented inside of the while loop. Next, I'll want to display the round number at the beginning of each round. And I'm going to put that right here above where I display the scrambled word. That should take the game through five rounds and display a round number. Let's check it out. So, I've got my round number. Let's type hint for a hint. I'm getting a 500 error, which means the blank is down again. So, I think the word is server. So, we are going through the rounds. Notice that the round number isn't incrementing. And that is because we still need to make sure that that number is incrementing. So way down here at the bottom of my loop after we've done everything, I'm going to say round plus equals 1. Let's control C to get out of this and run it again. So we've got round one. Let's get a hint. The word is debug. Now we're at round two, which is great. Let's get a hint. I think the word is deadline. And then we've got round three. Let's just keep going here. Let's skip to make sure that we get to round four. I'm noticing that it would be nice to have some space here. So, I'm going to go ahead and add that. Let's skip one more time to get to the fifth round. And let's try to guess this one. The word is meeting. Round five was reached. So, the game exits. So, it's working just as we'd expect. So, that's great. For aesthetic purposes, I'm going to add some space here. I'll add a blank line above and below round and another blank line after the scrambled word. Let's go ahead and take a look at this. All right. It looks a lot better with the spacing. So, go ahead and quit. And let's do the final step, which is to add the ability for the player to exit out of the game. First, to my options menu, I'm going to add the option to quit. So, we'll add it right here at the end. I'll make sure to add it to this one, too. You can guess skip or quit. Here in my if else chain, I'm going to add an L if that checks if the player typed quit. I'll go ahead and change this to an L if and then we'll start with if guess equals quit. Let's print thanks for playing. And we will break to exit the loop. Let's check this out. Type quit. It says thanks for playing and exits out of the game. Excellent work. Really fantastic. As you play through your game, you might notice that some words keep coming up more than once. That's because random choice picks randomly each round with no awareness of what it's already picked. We're going to fix that a little bit later, but for now, let's keep going. So far, we've made lists by writing out all of the items at once. But a lot of times, you don't have everything up front. You start with an empty list and add to it as your program runs. whether that's items a user types in, results you get back one at a time, or things that happen while the game is playing. The append function is for adding to a list. So, let's take a look at it. Let's say I'm building a simple task tracker or to-do list or something like that. I'll start with an empty list called tasks. And I can use the append function to add items to an empty list as they arrive. So, I can say tasks.append send invoices. Then we can use append again to add a second item. We'll also add a third item. Now I will print tasks. All those items have been added to the list. Append changes the list directly. So I don't need to save these to a variable in order for it to stick. This may seem a little bit strange. You might be asking yourself, why don't you just add them up here? Send invoices, book the venue, and so on. Well, say you were building a to-do list and you wanted the user to be able to add items to a list so that you could display it back to them. So, I'm going to make a prompt called task item and I will ask the user for input. Then I can say tasks.append task item. Let's see what this looks like. So, I want to add an item. Let's say by pencils. And as you can see, that ends up at the end of the list. Let's add another one just for fun. You can see that call printer repair is in the list. Notice that by pencils is gone. And that's because our data doesn't persist. It kind of starts over every time we rerun the program. Persisting the data so we can keep adding the list is a whole subject in and of itself, which we'll get to much later in the course. But for now, I just wanted to show you a practical example of how you might use append in a program. Because with just a list, an input, and a function like append, you're already halfway to making a to-do list application. As you'll see soon, append is going to come in handy in our word scramble game. It's going to help us keep track of words that have already been used so we can avoid repeating them. We'll get to that, but first, your challenge. Your challenge is to imagine that you're building a multiplayer game wherein players join the lobby one at a time. So, start with an empty list called lobby. append these players in order and then print the final list. Go ahead and get started. To complete this challenge, I'm going to start with an empty list called lobby. Then I'll go ahead and append these players one by one. I've appended all the players. Now I will print lobby and let's check it out. There we go. My list of players. Fantastic work. Let's move along. We've learned how to keep a while loop running with a counter that stops the loop once it reaches a target number. A while loop can also be driven by any condition that's true or false. Here we're going to see how to use a while loop with a membership test, which is just checking whether a value is in a list. To demonstrate, let's say I'm building a bingo caller. We have bingo numbers, which holds every possible call, or at least we're pretending it does for the purpose of this demonstration, and already called, which tracks the bingo numbers that have come up so far. The in keyword will let me check whether a bingo number is in the list already called. So, let's check if B7 is in already called. That gives me true because yes, B7 is already in the list. If we try to look for a bingo number that hasn't come up, let's say 068, that will give me false. Remember that a boolean is a Python data type that can only have two values, true or false. The in keyword returns a boolean, which means you can use it anywhere a boolean works, including as a while condition. If you've played bingo, you'll know that a bingo caller should never call the same bingo number twice. From the list of possible bingo numbers, I want to pick a random number and keep repicking until I land on one that hasn't come up yet. Let me show you what I mean. First, I want to pick a random bingo number from the list of possible bingo numbers. I'll import random, then create a variable called pick, where I'll use random choice to choose a random bingo number from the bingo numbers list. Now I'll create a while loop and I'll say while pick in already called I want to pick a new random number. What this says is while my pick is already in the already called list go ahead and pick another random bingo number. So let's say that I pick a random number and the program randomly picks I22. We'll look and see is I22 in already called? Yes, it is. So this condition is going to evaluate to true. It's true that we've already chosen this number. So go ahead and pick a new number. Then let's say it randomly picks B7. Now it's going to say, is it true that B7 isn't already called? Yes, it is. So let's choose a random number again. Let's say this time it chooses 068. Is it true that 068 is in already called? No, it's not. So, we know that we have a unique bingo number that hasn't already been called. So, instead of picking a new one, we will break out of the loop and we can be confident that we have a unique bingo number. So, now I can print my fresh bingo number and then let's append that number to the already called list. And that way, the next time we need to pick a number, 068 will be in the list and we'll know not to pick it again. As you can see, it happened to randomly pick 068, which is indeed a bingo number that hasn't already been called. If I were to print already called here, you can see that it has randomly picked B3, which is not in the already called list, but because we used append, it has added it to the list. In a while, we're going to use this mechanism in our word scramble game. We're going to pick a random word, check if that word has already been used, and if it has, we'll keep picking until we find a fresh word. And that way, as the game is going through its five rounds, we can prevent it from showing the user repeating words. But first, I want to give you a challenge. Your challenge is to build a feature that manages a charity raffle. The program picks a random winner, but make sure that nobody wins twice. If the random winner has already won a prize, keep drawing a random winner until you land on someone new. So, I'm also going to copy and paste a couple of lists. And your first step will be to draw a random name from the entrance. If the name you draw is already in the winner's list, keep drawing until you get somebody who isn't. Then finally, add the new winner to the winners list and print the name of the winner. I know you can do it. Go ahead and get started. I'll start by importing random. And then I'll choose a random name from the entrance list and save it to a variable called name. Then I'll say, well, name in winners. In other words, if my random choice is already in the winners list, I'm going to pick a new random choice. I'll keep doing that until I find a random choice that is not in the winner's list. I'll append them to my winners list and finally print the winner's name. Let's make sure this works. So, I've got a slight typo here. Let's clear and try again. We have successfully picked a winner who has not yet won a prize. The random choice function picks a word randomly from the word bank and then that's it. It's done. It doesn't keep track of what it's already picked. That means that as you're playing through the rounds of the word scramble, there's a risk that the same word will show up twice or even multiple times over the course of a single game. that's not going to be a very nice playing experience. So, your task will be to fix this by tracking the words that have already come up and making sure they don't repeat. So, we want to ensure that a word shows up no more than once per game. Outside the game loop, make an empty list called used to track already used word hint pairs. Inside the game loop, write another loop that checks if the picked pair is already in the used list. And the while loop should keep repicking until it finds an unused pair. Once you found a fresh pair, append it to the used list so it won't come up again. And finally, test your game to make sure it plays five unique words. Go ahead and get started on the challenge and good luck. Step one, I'm going to create an empty used list. Then inside the loop, here's where I'm choosing my original word hint pair. Under that, I will start my second while loop, which will check if the word hint pair is in the used list. If the chosen word hint pair is in the used list, I want to go ahead and pick again. So, we'll ask it to choose a random choice from the word bank and unpack it into word and hint. When a fresh pair is found, I want to go ahead and append it to my used list so that it won't be chosen again. Let's run and make sure we get five unique words. So the words that came up were prototype, deadline, deploy, debug, and server. That's five unique words. So this is working just as expected. One thing that is worth keeping in mind is that this repicking loop only works because the word bank is larger than the number of rounds. We've got 10 words and five rounds. So there's always a fresh word left to find. But say we had a word bank that had exactly five words and a game that had exactly five rounds. By the last round, every word is already going to be in the used list. This would cause the inner loop to keep picking used words over and over again forever, never being able to find a new one, and so the game would just freeze up. It would hang. So whenever you use this pattern, make sure the pool you're picking from is bigger than the number of items you'll pull out of it. Really fantastic work here. We are so close to being able to call this game finished. You've built a scrambler, added hints, set up a game loop, and made sure words don't repeat. You've done an incredible amount of work, and your game is almost complete. There's just one feature left, and that is keeping score. Before we get started, I want to quickly point out that I've added a little welcome screen to the game. It's just a few print statements, but it helps the game feel a little bit more complete and it will be fun to customize. But first, your challenge. You're going to want to tell the player their score and how they did. Before the game loop, you're going to want to set up a score variable to count the number of correct guesses. And go ahead and start by initializing that to zero. Each time the player guesses correctly, you're going to want to increment the score by one. A little hint here, if you don't know where to increment the score, go ahead and look for a place in your code where you're already comparing the player's answer to the correct answer. When the game ends, I'd like you to show a final score out of the total number of rounds. And here's an example of what that should look like. So, in this example, they've gotten a score of three. So, you'll display a message that says final score three out of five. Then you'll use an if l if else chain to give fun feedback based on the player score. I've included some example output, but I want you to feel free to write your own text. So for a score of five, we want to let them know that their score was perfect. Or a score of four, we want to let them know that they were almost perfect or did great. With a score of three, we want to let them know that they did fairly decently. And a score under three is a try again next time. go ahead and get started on this challenge. I hope that went really well and you're excited about your finished game. Let me show you my solution. First, I'll come down here and add a new variable called score, which I will initialize to zero. Then, I'll come down here to this if else chain. And here's the block where I am comparing the player's guess to the correct word. This is where I determine whether the guess is correct. So, this is a great place to increment the score. So, here I'll say score plus equals 1. So, every time the player guesses a word correctly, it's going to add a point to the score. Next, at the very bottom after my game exits is where I want to add my score feedback code. I'm going to pay extra attention to the indentation here to make sure that this code is outside of the loop. I will print my final score and I'm going to display whatever their score is. And here I'm actually going to use the constant rounds. I could very easily just put a five here, but just in case I someday want to change the number of rounds. This will give me a little bit more flexibility. Next, I'll start writing my if chain that'll give feedback based on the score. So, if the score equals 5, which is the maximum score, I'm going to keep things simple here and assume that my game is always going to have exactly five rounds when I score. But if I wanted to make it a little bit more flexible, I could base this on the number of rounds in the game because the number of rounds in the game is always going to be the maximum score. So, if there are seven rounds, they can score a maximum of seven points. And that would keep the score relative to the number of rounds. So if we were to continue in this vein, the next conditional might be else if score equals rounds minus one. So if we had seven rounds in the game, that would mean we'd be checking here if their score was equal to six. Rounds minus one is six. So that's something to think about when it comes to making your games flexible and maintainable. But for right now, let's not make things too complicated. We'll assume five rounds in the game. So if the score equals five, I'm going to go ahead and print my message, which I'll copy and paste in. Else if the score equals 4, I'll paste my second message in. The score is three. I'll print my good effort message. And now the remaining possible scores are 1 or two. And I'll let else cover both of them. So unless the score is three, four, or five, we'll say better luck next time. For aesthetic purposes, I'm going to add a couple of blank print statements. And let's make sure this works. Here you can see the welcome screen. We've got our first word. I've played through the game. And as you can see, we have a final score of five out of five. And a little message here to wrap things up. And that's the last feature. Your word game is complete and fully playable. Congratulations. You have done it. You've built a finished word scramble game with a lot of moving parts. And I hope you're extremely impressed with yourself. I am certainly very impressed. You've learned a lot in this section of the course. Lists and tpples, the random module, while loops, conditionals, and string methods. That's a solid stack of Python skills. I want to encourage you to have fun making this game your own because I think one of the funnest things about it is how customizable it is. You could completely rename it and change the theme. Mine is tech jargon, but yours could be about animals, movies, sports, mythical creatures, whatever else sounds fun. You could expand or completely replace the word bank. You could ask your favorite chatbot to generate hundreds of word and hint pairs to create a massive game where words rarely repeat. You could alter and improve the game's appearance by changing the welcome screen borders or adding emojis. There's really so much that you could do. So have fun, be creative, and I'll see you in the next one. Picture a karaoke night. Crowds are signing up to sing. Groups of singers are constantly coming and going. Some beg to be bumped to the top of the queue. Whatever happens, the host has to keep track. And in this module, we're going to build an app called Sing Out to help them do it. So, let's take a look at the finished app. As the developer, you'll give the karaoke host a complete set of commands to manage the queue with. And notice the queue is numbered, so every singer will be able to see where they are in line. Let's say a couple of people walk in wanting to sing. So, the host adds them to the end of the queue. I can do that by typing add, and then it'll ask for a name and a song. Let's add one more. Notice that the app formats the names and songs for us. Let's say it turns out it's Mike's birthday. So, the host wants to bump him up to the top of the queue. Can type top. Then, it's going to ask me for the number of the person in the list. I want Mike, so I'm going to type four. And that bumps Mike to the top of the list. If the karaoke host wants to call a singer up to the mic, they can type next to bring up the next singer. Now, the singer is removed from the list and announced here. A karaoke night is pretty loud and hectic, and the host is bound to mistype things. So, we're going to build this app with some guard rails. Let me show you. Let's say I want to move someone else to the top of the list. And it's asking me for a number, but let's see what happens if I type a word. It doesn't crash. It just asks for a number. and the app carries on. What if I call singers up when there's nobody left in the queue? I'm going to call the singers up until the list is empty. You'll see now it says the queue is currently empty. If I try to call next again, it simply repeats. The queue is empty. No crashing. So, this is what we'll be building in this section of the course. By building this project, we are going to learn about functions. You've been using functions since your very first Python program. Every time you've called print or input, you're using a function that comes built into Python. But now you're going to learn how to write your own functions. Every command the host types in the Karaoke Q app is going to be a function that you write. We're also going to go deeper into planning, scaffolding, and organizing application code, which includes an important concept in software development called separation of concerns. The idea is to use functions to separate the different kinds of tasks your program needs to accomplish. Instead of writing code top to bottom the way we have so far, you'll learn about another type of loop called a for loop, which lets you run the same block of code once for every item in a list. Very useful, as you'll see. We'll learn a few more list methods to help manage the karaoke queue, like removing singers from the queue or moving them to a different position. Finally, we'll learn how to handle bad input and prevent the app from crashing with guard clauses and try except. A guard clause is a check at the top of a function that handles when something goes wrong. And try except lets us decide what to do when a block of code fails. As usual, we'll build the project in stages with lots of opportunities to practice along the way. Are you ready? Let's go ahead and get into it. Let's talk about functions in Python. A function lets you save blocks of reusable code. Let's say I'm building a game that involves a dice roll. To simulate a dice roll, I'll first start by importing random. And then I'm going to use a function available on random called rand int. rand int takes two numbers. I'm going to pass it a 1 and a six. And it's going to return to me a whole number between 1 and six. And I'm going to save that to a variable called roll. Then I'm going to print the random number as a roll. Let's run this. And you'll see that every time I run the file, it'll give me a random roll. Now imagine I was building a game that required a dice roll over and over and over again. Instead of repeating these two lines of code everywhere, I can package them up into a function. Here's how you make a function. A function starts with the keyword defaf, and that is short for define. This tells Python I'm about to write or define a function. Now we can give the function a name. I'm going to name it roll dice. Naming a function is very similar to naming a variable. It should be lowercase words joined with underscores. And the name should describe what the function does. So just like with a variable, we want to avoid naming functions that don't mean anything like fu or x. After the function name, we need parentheses and a colon. Then I'll move my code into this function by making sure that I indent it. So this is another place where indentation is really important. Whatever I indent inside the function is called the body of the function. To run the code inside of roll dice, I'll need to call the function. We've seen this many times before when calling built-in Python functions like print for example. So I'm going to call my function using its name followed by parenthesis. So just to be really clear, this is me defining a function and this is me calling a function. So here I'm saying whenever I call a function called roll dice, here's what I want you to do. And now I'm calling roll dice. So let's run this and see what happens. I rolled a one. The fun and useful part about this is that I can call this function over and over and get a new dice roll each time. I can also call functions inside of functions. So say I'm building a game where I need to throw two dice at once. I'll define a function called throw dice and inside I'll roll the dice twice. Now I can call throw dice and each time it's going to give me two rolls. So as the programs you write get more and more complex, you can use functions to neatly organize your code into different reusable tasks. So for example, roll dice is one task. Throwing multiple dice is another task. And another might be displaying a welcome screen. I'll define yet another function show welcome and in it I will print welcome to dice fight. Then maybe to create a little space I'll add some empty print statements. And then you might wrap all these functions into yet another function called play game. And inside of play game I'll call show welcome and also throw dice. And now if we call play game, instead of having to repeat multiple lines of code all over my game, I've wrapped it up neatly in a function I can call with one line anywhere I need to, as much as I need to. So functions are extremely important when it comes to keeping your code organized and maintainable. And we'll be talking much more about them in the scrims to come. But first, let's give you a chance to practice writing some functions. You've very generously decided to pick up coffee for six of your favorite co-workers this morning. So, first I want you to write a function announcing your coffee run. And this is your example output. You're going to see this a lot from now on. I'm going to show you an example of what your function should produce. So, feel free to be creative with what the text actually says, but your function should print something to the effect of, I'm headed to the coffee shop. Who wants a latte? Step two, write a function to calculate the total cost if you buy each of your co-workers a latte for $5. So, this function will print out six lattes comes to $30. Notice it says, calculate the total cost. So, go ahead and do some math inside of your function. And step three, your co-workers thank you profusely. To save yourself a little time, write a function that prints you're welcome twice and then call it as many times as you need to to thank all six of your co-workers. And your final step will be to put all your function calls in a new function called coffee run and call it to start your coffee run and to make sure that everything works. Go ahead and get started on this challenge. We're back. I hope that went smoothly and you're properly caffeinated. Let me show you how I wrote my functions. Step one is to write a function announcing my coffee run. So I'm going to name my function announce coffee run. I'll put a print statement into the body of the function and I'll call my function really quick just to make sure it works. Looking good. Let's move on to the next function. In the next function, I'm going to calculate my coffee total. So I'll name my function calculate coffee total, the little abbreviation. In this function, I'm going to start by defining some variables. So, I'm going to say I have six lattes, and I'm going to say my cost per cup is five. And then I will print an f string with a number of lattes and my total, which would be lattes times cost per cup. If you did this a little differently where your variables are outside of the function, this is also totally fine. Let's go ahead and call the function and make sure it works. That also works just fine. And we'll talk a lot more about variables and functions later in the course. Let's move on to the third function where we need to say you're welcome to our co-workers. I'll make a variable called say welcome where I'm going to print you're welcome twice. And we have six co-workers. So I'm going to call say welcome three times. There's our six you're welcomes. What a timesaver. And now let's write our final function which is to put all of these functions in one function called coffee run. So I'm going to come up here and define a function called coffee run. I'll indent my say welcomes and above that I'm going to call first I'll announce my coffee run and then I will calculate my coffee total. Let's run this a final time and you'll see that I have actually forgotten to call coffee run and I have a little typo. So always make sure that you've called your function. So now that we're definitely calling coffee run, let's try this again. And there is our entire coffee run. Up next, we have even more about functions. So please join me in the next one. Say I'm building an app and I want to greet users by name when they log in. With what we know so far, I'd write a function for the first person. So let's say we want to greet somebody named Jenny. So I have my function greet Jenny and in it I will print welcome Jenny. And then I'll need to define another function for the next person. And in this function I'll print welcome Marcus. So this isn't going to work because a thousand users would mean a thousand nearly identical functions. So let's not do that. Instead I'm going to define a function called greet and I'll pass it a parameter called name. A parameter is essentially a variable. So I'm saying to Python, when I call this greet function, I'm going to pass you a name to use inside of the function. Inside of the function, I can use name like I'd use any other variable. So I'm going to make an F string that says welcome. Then I'm going to drop that name right in there. So I'll call my function greet. Then I will pass it the string Jenny. Let's run the file. And it says welcome Jenny. The awesome part about this is I can call the greet function again with somebody else. Thanks to my name parameter, I can use this one function over and over to greet an infinite number of users. Something worth calling out here is the difference between a parameter and an argument. You define a function with a parameter. When you call a function, you pass in arguments. Another way to put it is when you define a function, parameters are your variables. And when you call a function, arguments are the values you're assigning those variables. You can define a function with any number of parameters. I can pass a second parameter here called unread messages. Now I can use both parameters inside my function. So let me expand this to say you have unread messages. This will be a number. So it'll say you have x number of new messages. Now when I call the function I'll pass in two arguments. They line up in order. So the first argument goes into name. So I'll say Jenny again. And the second goes into unread messages. Let's say 15. Let's go ahead and run again. Welcome Jenny. You have 15 new messages. So the order that you pass arguments is important here. If I were to flip these values. So let's say I put in 15 first and then Jenny. It's going to say welcome 15. You have Jenny new messages. So it's important that you pass arguments in the same order that you've defined your parameters. I'll go ahead and switch this back. The variables, these parameters and their assignments are only for this function. That means that if outside of the function, I'm going to comment this out. If outside of the function I were to try to print the variable name, Python has no idea what I'm talking about. It doesn't know what name is, I get this error because name and unread messages are only available in what's called local scope. That means that only this function has access to name and unread messages. If I define a variable outside the function, it's in what's called the global scope. So I'm going to define a variable called shop name. and I can make a function that reaches out and works with that variable. So I'll make a function called show welcome and we'll print an fstring that uses that variable. When I call the function, you can see that my function does have access to this variable. That's because this entire program has access to this variable. It's in what's called the global scope. So the idea that I'm trying to get across here is that the parameters and variables that you use inside of a function are only accessible to that function. A function can reach out and use variables in the global scope, but nothing can reach in and access the function's parameters. So you might be wondering why bother using parameters if a function can just access variables outside of itself. And the reason is because they help keep a function contained. I know that I can use this great function anywhere because it doesn't depend on any variables in the global scope. It comes packaged with everything it needs or you'll pass in the arguments it needs when you call it. Because the function doesn't depend on any outside variables that frees you up to use it in multiple files or in multiple areas. I'm going to go ahead and comment all of this out so I can give you your challenge. Your challenge is to write two functions with parameters. For the first one, I want you to write a function called add numbers that prints the sum of two numbers you pass in. And I want you to call it a couple of times with different numbers. So here's an example output. If you give it the numbers 2 and 3, it should print five. If you give it the number 10 and the number 45, 45 + 10 is 55, so it should print 55. Function number two, you've scrambled a word before. Now, I want you to create a function that can take in any word and scramble it. So, remember to scramble a word, you turn it into a list of letters, shuffle those letters, and then join those letters back together into a list. So, let's make that reusable. Write a function called scramble that takes in a word and prints a scrambled version of that word. So, for example, if you call it and pass the argument Python, it might print a scrambled version that looks like this. go ahead and get started. First, I'll define a function called add numbers. And my function will take two parameters, which I will call number one and number two. And then I'm going to print num one plus num two. And that should print the sum of those two numbers. So, I'm going to call add numbers with the numbers two and three. So, that should be five. And I'm going to call add numbers again with 10 and 45. And that should print 55. Let's make sure my function works. There we go. It prints 5 and 55. For the second one, I'll create a function called scramble. And to scramble, I'm going to pass in a word. And inside the function, I'm going to split the word into a list. And I'm going to save that list to a variable called letters. Then I'm going to randomly shuffle the letters. And once those letters are shuffled, I'm going to join them together with a blank string so that it's all one word. And to join, I will pass my letters list. And I'm going to go ahead and print the result of joining these back together. Now, I'm going to call my scramble function with my own name. Why not? And we run that. Looks like we need to remember to import random. So, at the top of my code, I'm going to import random. And let's try this again. We run this a couple of times. There are scrambled versions of my name. You've already used built-in functions that hand you a value. A good example of this is the list function. The list function breaks a string into individual letters and then returns you or hands you back a list which you then save to a variable. So, it doesn't just print the word listen as a list. it actually handed you back that list so you could save it to a variable and do something with it. Now, let's write our own functions that return values rather than just printing them. Say I'm planning a pizza party and I want to work out how many pizzas I need to order. I'll start with a function that calculates how many slices I need. I'm going to pass in the number of guests and I'm going to estimate that each guest will eat two slices. So, I'm going to print guests* 2. Then I'll run my function assuming I'm expecting 10 guests. And that gives me 20 slices of pizza. Now I'm going to write a second function to figure out how many whole pizzas I need because you can't order half or a quarter of a pizza. First up here, I'm going to import math so that I can use math. And in my function, which I will call pizzas needed, we'll have that take in the number of slices. And we're going to say each pizza has eight slices. So, I need to divide the number of slices I need by eight to get the total number of pizzas, which I will round up to the nearest whole pizza. And then I'm going to do something different now. I'm going to have the function return this value. So, to slices, I'll save the result of calling slices needed. I'm going to say I need 10 slices again. And then to a variable called pizza, I'm going to save the result of calling pizzas needed. And I'm going to pass in the number of slices. So basically I need this value. Going to save it to a variable so that I can pass that value to the pieces needed function. Then finally I'm going to print the number of pizzas that I need. Let's run this. And maybe some of you have already guessed what's going to happen. Mhm. It broke. The problem is this print statement right here. I can't just print this value. I need the slices needed function to actually hand me that value back so that I can pass it to pizza slices. So the return statement is how we get a value out of a function so that we can use it rather than just display it. So if instead of printing I return here. Let's see if that works. It tells me I need three pizzas which is correct because I need 20 slices. 10 guests eating two slices each is 20 slices. divided by the eight slices in one pizza. That would be two and a half pizzas, which it rounds up to three. So, the difference between print and return is that print shows you a value on the screen in the terminal. Return gives the value back to your code so that you can save it and use it. So, I'm going to comment this out and give you a challenge. Your challenge is to write some functions that return values. For each function, save the result to a variable and then use the result to print an fstring. First, write a function called room area that takes in a length and a width and returns the area of a room. I want you to get the area of a 12x10 room. And your final output should look like this. The room is 120 ft. For the second one, write a function called add tax price that returns a price with an 8% tax added. And you can calculate that by multiplying the price by 1.08. Use your function to get a total for a $50 item. And this is what your final output should look like. Third, write a function called full name that takes in a first and a last name and returns the first and last name joined with a space. Then use your function to build a name and print a greeting with it. And here is what your final output should look like. Go ahead and get started on the challenge. Okay, let's do this first one. I'm going to start by defining a variable called room area. And to it, I will pass a parameter called length and a parameter called width. Then I'm going to return length time width calculating the area. And then to a variable called area, I will call my room area function. And I'm going to pass in 12 and 10. Then I'm going to print an F string that says the room is area square ft. Let's make sure this works. There we go. Moving on to the second one. I'll define a variable called add tax. Pass in a parameter called price. And then I'm going to return price time 1.08. I'll have a total variable where I'll call add tax and I will pass in 50. Then in my fring I'll say with tax that comes to total. Let's double check this one. With tax that comes to $54. Just for fun I'm going to add a colon.2f here just so the formatting looks a little bit nicer. Now on to my final function called full name that takes in a first and last name. Then I'll return first plus a space plus last. Then in my fstring I will say welcome name. But I need to make sure to create a variable called name and call my function in it. Let's test our final function out. There we go. Welcome paraborggan. Go ahead and make sure not to forget my exclamation point. And there you go. Beautiful. Let's get a basic skeleton up and running for the Singout karaoke Q manager. When you strip the karaoke theme away, this app is really just about managing a list. Adding items to a list, removing items from a list, and moving things around the list. Managing a list is the basis of many application features. It could be a playlist, a to-do list, or an online shopping cart. The underlying ideas are very similar. So our list is a singer's Q and each singer is a name and a song paired together. I've also defined here a run app function which so far takes in the queue and prints a banner that says welcome to Singout Karaoke Q manager. So let's think for a second about the main things that this app needs to do. As we're thinking about how it will run, we know that it needs to show a list of commands. We know we want to be able to show the list of singers. We know we need to be able to respond to user commands. So what commands do we need to respond to? We want the user of our program to be able to add a singer, remove a singer, and quit or exit the program. Another way to state this is that the app needs to keep running until the host decides to quit. You may remember that the word scramble game ran for a set number of rounds, but this app is a little different because we want the user to be able to run it until they decide to quit out of it. So instead of counting rounds, I'm going to use a variable to track whether the app is still going. So here inside of my run app function, going to create a variable called is running, which I'll set to true. And still inside of my run app definition, I'll create a while loop that checks for the condition is running. So as long as is running is true, this loop will keep going. If I want it to keep running until the user quits, I'm going to have to listen for a command from the user. In other words, I'm going to have to ask for some input from the user. So, I'm going to create an input prompt, and we're going to change this. But for right now, I'll just say, do you want to quit? I'll save their response to a variable called command. And now, I can listen for that command and respond to it. So, if command equals quit, I'm going to change is running to false. That will change this variable up here to false, which will cause the loop to stop running and the program will exit. I'm now going to run run app passing in the queue. And let's run this. So you see our banner, do you want to quit? I'm going to type quit and it says good night and exits the program. So we know that's all wired up. What I want to do now is instead of saying do you want to quit? We want to listen for several types of input. So, I'm just going to add a little carrot here to indicate that I expect the user to type something. And then above that, let's add an options menu. So, we'll say options. By the time we're done with this, we want the option to add a singer, remove a singer, or quit. I'm going to say the options are add, remove, and quit. Now, let's start stubbing out our functions. I'm going to show you an example before I hand the rest over to you. going to move this list up here because this is where we're going to start stubbing out functions. I'm going to delete the ones that we've done. So, we are showing a list of commands and let's take a look at this. As you can see, here are my options and the only one that works so far is quit. So, we can delete this line and we've also added the ability to quit or delete the program. And so, what we are left with are the functions that we need to stub out. I'm going to do show Q. I'm going to make a function called show q and I will pass in the Q and then I'm going to give this a dock string. A dock string is very similar to a code comment, but usually it's a sentence in triple quotes that describes what the function will do. So in my triple quotes, I'll say that this function is going to print everyone currently in the queue. In the body of the function, we're not going to write it yet. Instead, I'm going to print a placeholder in square brackets, and it's going to say the Q goes here. And the reason that I've done that is now here in my game loop, right before the options menu, I can call show Q, passing in the Q. I'm also going to add a little white space here just to make it a little bit easier to read. And if we run this, we can see my little placeholder here. So you are going to do something similar with the add singer function and the remove singer function. So that you'll be able to run the app and a little placeholder will show up here and you'll add remove and a placeholder will show up here and it will look something like this. We've got our banner, we've got our Q goes here because we want to keep showing the Q after any command is typed pretty much. So, you'll be able to type add and see add a singer. And if you type something like sing, the app will responds, sorry, I don't know the command sing. And if you type quit, it will say the queue is closed. Good night. So, we're not fully building out any of the functions yet. We're getting the app running with empty placeholder functions. So, we can start it up and see the commands working and make sure that everything is properly wired up before we start filling in the logic of the functions. This is called scaffolding or building an app shell. So you get a skeleton up and running first so that you're able to build a more complex app while still making sure that everything works along the way. So here is your challenge. It's a little bit long as is the example output and that's why I put it in a different file. So the app has the show q function stubbed and the quit command wired up. Two more functions need stubs and the loop only handles a quit command so far. So what you'll need to do is stub add singer and remove singer. And you'll do that by following the show Q example. Each dub will take in the Q as a parameter will have a dock string describing what it does and it will print a placeholder. Then I want you to finish this if l if else chain so that it handles the other two commands. So when the command is add or remove, you'll need to call the matching functions. Then to the end of your chain, you'll need to add an else that handles when the app receives an unrecognized command. So that's what I showed you down here. If you type a command that it isn't wired up for, like sing or banana or apple, it's going to say, "Sorry, I don't know that command." And then finally, run the app and test it. Make sure add, remove, and quit each work. And then if you type a command that doesn't exist, it will print a message instead of crashing the app. So, your two function stubs will go here, and your commands will go here. Go ahead and get started on this challenge. You've totally got this. All right, I hope that went really well for you. Let me show you how I did it. So, I'm going to make a little room here. And first, I'm going to define the function remove singer. I'll pass in the Q, and I'll add a dock string that says remove singer from the que. We'll go ahead and print the string remove a singer then and actually I'm going to put my next function above this and that is my add singer function then I will add a dock string that says ask for a singer and add them to the que and I'll print my string add a singer. So down here I want to listen else if the command is add I want to call the add singer function. Else if the command is remove I want to call the remove singer function. Else if we get a command that we don't recognize I'm going to say sorry I don't know the command whatever they typed in. Now let's check and make sure everything's wired up. So we see our banner. Here's where our Q will be. Now let's type add. The add a singer function ran which is great and we're redising the Q. Now let's try typing remove. The remove function ran and we're again showing the Q. Now let's try a command that we don't recognize. And now I described this as a crash in the instructions and it's not exactly a crash. It's more like a silent failing. So before they could type in an unrecognized command and absolutely nothing would happen which is just as bad as a crash. But now we have a message informing them, hey, sorry, I don't know the command that you typed in. So that's all great. I'm going to quit out of this. I'm going to make this goodn night message a little bit more descriptive. So we have a beautiful skeleton here that we can keep improving upon. Really, really fantastic job. You've used a while loop to keep code running until a condition is met. I want to introduce you to a new kind of loop called a for loop. A for loop runs a block of code once for every item in a list. So let's say I have a list of party guests. Let's loop over the list and individually print each name. I can do that with four guest in guests. then indented so it's inside of the for loop. I'll print each guest. Let's see what that looks like. And you'll see it prints every name in the list. So in English, you can read this code as for each guest in the list called guests, run this indented code. Just like other loops, indentation is how Python knows this code is inside the loop. This is called the body of the loop. When this code runs, Python starts with the first item in the list. It'll run this code which says print that item. So it'll print Marcus, then it moves on to the next item in the list, runs the code, and so on until it reaches the end of the list. So it's always going to run exactly as many times as there are items in the list. The word guest here is just a variable that represents each item in the list. So I could, for example, drop it into an F string and format it however I want. So I could say something like welcome guest and that is going to print for every guest in the list. It's worth mentioning as well that this needs to be the name of the list that you want to loop through. But because this is just a variable, you could really name it anything. You could call it for example person and guests or item and guest or you could even name it something totally meaningless like for fu and guests. But it is convention and also best practice to give it a meaningful name. And a lot of times the list will be a plural and this variable will be the single of whatever the plural list is called. If I want to continue writing code after my loop, I'll need to make sure that it is not indented. So it should line up with the leftmost code. And that's how Python knows that it's outside of the loop. So if after I'm done printing the loop, I wanted to say something like that's everyone, then you'll see that will print once when the loop is finished. And if I were to indent this line, then Python would assume that it's inside the loop. So it would print once after every item in the list. I'm going to move this back out of the loop and comment this out so that I can give you your challenge. It's awards night and here are the nominees for best new app. The list of nominees can grow or shrink year to year, so your program shouldn't care how many names are on the list. What you'll need to do is loop over these nominees and print each one on its own line. And I want you to format it like this example. So it should say nominated for best new app plus the name of the app. Then after the loop, print one closing line that says and those are your nominees. And that should print just once after all the names. Go ahead and get started. To loop through the nominees, I'm going to say for nominee in nominees. Then in an F string, I'm going to print nominated for best new app and then the name of each nominee. And outside of the loop, I'm going to print and those are your nominees. Let's check this out and make sure it works. So, as we move through this section of the course, you'll see that loops come in very handy. You can write some code once and run it for multiple items, whether there are three items or 300. And that helps the apps you write do a lot more. A karaoke Q manager is going to need to manage a list of tpples, a singer who is matched with the song that they want to sing. So let's talk about how to loop through a list of tpples. So, I've got my list of party guests, but now each one is bringing a dish. So, I'm storing both the name of my party guest and the dish they're bringing together. I'm still going to call my loop or guest in guests. But this time, the guest isn't just a string containing the person's name. It is a tpple. So, inside the for loop, I can unpack a tpple just as we have done before. So, I'm going to unpack the tpple into name and dish. And then I can go ahead and print both the name and the dish in an fstring. So I'll say name is bringing and then the dish they're bringing. Let's make sure this prints. Okay, great. So for each pass through the loop, Python hands me a tpple. I unpack it in the body of the loop and then I've got both values to work with. Before we move on, here is a chance to practice this. Each act in this festival lineup is a tpple. So, we have the band, the genre, and how many minutes they are going to play. Your challenge is to loop over the lineup, unpacking each act into band, genre, and minutes. Then you'll want to use if else to print a long set line for any act playing 90 minutes or more. And the rest will have a normal line. So, here's an example of what your output should look like. So, go ahead and give this a try. Before I show you the solution, I'm going to come up here and comment this code out. I'll begin by writing my for loop. I'm going to name this a little bit differently. I'm going to name it for act in lineup. And I'll unpack the act tpple into band genre and minutes. I want a different message of the band is going to be playing for 90 minutes or longer. So I'm going to say if minutes is greater than or equal to 90 I want to say that they are doing a longer set. I'll start with the band and then in parentheses I'll put the genre plays a long set of however many minutes over 90. Else I will print my normal message. Let's give this a check. Looks like I have a little typo right here. Let's check this again. The 90-minut set and the 100 minute set display different messages and that's exactly what we were looking for. Really excellent work. Let's keep it moving. Congratulations. You've done an amazing job so far and you have the knowledge and skill to start displaying the karaoke cue. So, we're going to jump right into your challenge, and that will be to replace show Q's placeholder code with working code. So, here are your instructions. First, you'll add a header that says current Q, and give it some visual space by adding a blank line above and below. So, when you run your app, it should look something like this. Then, you'll want to loop over the Q and print each singer's name and the song that they want to sing. For the third step, I want you to move the options menu into the show Q function. Every time we display the queue, we want to remind the user what their options are and prompt them so that they can enter another option. Go ahead and get started. First, let's get rid of the placeholder code and print a header. So, I'll print a line that says current Q and some blank print statements above and below. Let's double check this before we move on. Great. There's our current Q banner. Now, what I want to do is loop through all of the tpples in this list and display them. So, under my banner, let's make a little room here. Under my banner, I'm going to make a loop where I'll say for singer in Q. Then, I'll unpack the name and the song from each singer. And in the example, you can see that we're printing each name with a dash in between. So, I'll create an F string that prints name dash song. Then, I'm going to grab my options menu and this empty print statement that is creating space. Then, I'll move that just below where I'm printing out the que. And let's take a look. I'll quit out of this, clear, and run again. We've got our header. We're displaying the Q. And we have our options menu and prompt. This is really starting to take shape. Good job, Let's give the host a way to add singers. So adding a singer is two separate tasks. Number one, prompting the host for a name in a song. And number two, appending that singer to the queue. So I want you to write a function for each. First to write a function called prompt for singer. And you can go ahead and add the new function right here below show q in prompt for singer. We want to ask the host for a name and a song. And the function should return the name and the song title cased and stripped of any extra spaces. And keep in mind here that a function can return two values at once if you separate them with a comma. And here's an example of that. Then when you call this function, you can get the values out by unpacking them the same way that you would a tpple, which is what you're going to do in the add singer function. You'll call prompt for singer, unpack what comes back and saved variables. Then once you have the singer and their song, you'll add it to the end of the queue. And you'll also print a message confirming that the singer was added. So here is your example output. We show the host their options. They type add. Then we'll show them a name prompt and a song prompt. But notice when it's added to the queue, it's in title case just like the rest of the singers and their songs. So we prompt the user for a name and a song. Once we've added it to the queue, we include a message confirming that we've added that name to the queue. Go ahead and get started on this challenge. First, let's write the prompt for singer function. prompt for singer doesn't need to take in the Q because it's not acting on it. It's instead prompting the user for a name in a song and then returning the name in the song. So to the variable name, I'm going to add a prompt that says name. And I'm going to make sure to strip and title case whatever I get back from the user. Then I'll prompt for a song doing something very similar, which I'll also make sure is stripped and in title case. Then I'm going to return name and song. To be consistent, I'm going to add a little dock string here that says ask for name and a song and return them cleaned up. Now in the add singer function, I'm going to unpack name and song, which will be the result of calling prompt for singer. Let's go ahead and print name and song just to make sure this is working correctly so far. So let's say we want to add a singer and I'll say Sam. Here is our print statement right here. So, you can see we're successfully getting the name and song out and the song name is title case even though we entered it in all lowercase. Now that I've confirmed that I'm successfully getting the name and the song, I can append the singer and the song to the end of the que. So, Q.append I'm going to append this as a tpple name and song. Then I'll print my confirmation added name to the Q. Let's give this a final check. And there is Bob and singing in the rain at the end of my queue. One thing I want to do here is fix this typo and add some space in between the prompt and our confirmation. Fix my typo. And I want to add a space here. I'll quit out, clear, and run again. And there we go. The confirmation message is a little bit more readable. Great work. Karaoke singers often change their mind about singing a song or head home early. So, the host needs a way to take them out of the queue. We can remove an item from a list with the remove function. So, I have here a list of contacts and I can remove an element from this contacts list by using a function called remove. And then I'm going to pass the value of the item I want to remove. So, let's remove Homer. Then I'm going to print the contacts list. And if we run this, you'll see that Homer has been removed from the list. So it's pretty straightforward. You tell it what item you want to remove and then it removes it. But there is one complication, which is when you try to remove an item that is not in the list. So there's no Alex in the list. What happens when we try to remove Alex? We get an error saying that Alex is not in the list. So, we'll need to account for the fact that remove is going to throw an error if it doesn't find what we're asking for. Before we try to remove an item, we'll need to make sure that that item exists. So, I'm going to define a function called remove contacts. And to it, I'm going to pass my list of contacts and the name that I want to remove. Inside of my function, I want to loop through this list of contacts and look for a name that matches the name that I'm going to pass in when I call the function. So, I'm going to loop through the list by saying for contact in contacts. Then, I'll say if contact equals name, I want to remove that contact from the contacts list. So, contacts.re the name in the list that matches the name that I've passed in. So in other words, let's say again that I want to remove Homer. I'm going to call this function. I'm going to pass in the list that I want to remove from. Then I'm going to say I want to remove Homer. So the function is going to loop through every item in the list. And it's going to check does the item that I'm currently looking at match the name Homer? And if it finds the value Homer in the list, it's going to remove it from the list. If it finds a match and successfully removes it, I want to confirm to the user that that item has been removed. So once we find a match, I'll print that we removed the name that they asked us to remove. If this whole loop finishes and no match is found, I want to tell the user that too. So outside of the loop, I want to print a message saying, "Sorry, we didn't find a match." I'll say, "Name isn't in your contacts." There is still a small problem here. I'm going to run this so you can see what it is. So, what happened is that it found Homer, but then the loop continues looking for another match. When it didn't find one, it ends and then the function moves on to the next line of code. So, what happens is that removed Homer and Homer isn't in your contacts both ended up running. The way we can prevent this is by adding a return statement inside this if. This is called a bare return and it tells the function to exit. We're using it to say once you find a match and remove it, stop. We're done. And because the loop stops immediately when it finds a match, it will only ever reach this print statement when the match isn't found. So now when we run, we simply get a message that Homer was removed. And if we try to search for somebody who isn't in the list, then we get one message that person isn't in your contacts. So, that is how we can remove an item from a list. And I want you to use what you've learned to fill in the remove singer function. Here are your instructions. You're going to find the singer in the queue and remove them. So, in the remove singer function, prompt the host for who to remove. And be sure to clean the input. We want it title case with no extra spaces. Then, you'll want to loop through the queue looking for a matching singer name. The extra challenge here is that in Singout the Q is a list of tpples, a singer name and a song. So you'll need to compare against the name inside the tpple rather than the tpple itself. And I've included a hints MD file over here if you need help. So go ahead and read it for some guidance. If you find a match, remove the whole tpple with the remove function. Print a confirmation and return. And finally, if the loop finishes without a match, print a message telling the host that no match was found. Here's an example of what that should look like. When you run the app, you'll prompt saying something like, "Who do you want to remove?" Person enters a name. Then behind the scenes, you'll remove that name and print a message that says removed Allen from the queue. You should be able to confirm that the singer was successfully removed because the queue will reprint without that singer. And this is what it should look like if you search for a name that is not in the queue. You prompt, who do you want to remove? The user types in Sarah and a message prints that says there's no one named Sarah in the queue. Go ahead and get started. I know you're going to do a fantastic job. Let's go to the Singout file and down to remove singer. I'm going to start by deleting this placeholder and prompting the user for some input. to the variable name. I'm going to save the result of asking the user, who do you want to remove? I'm going to make sure that I strip and title case the input. Then I'm going to go ahead and add a blank space here so that whatever messages we print below are a little bit easier to see. Now I will loop through my Q. So I'll say for singer in Q. And here, if you took a look at the hints file, it talked about how you can unpack a tpple like this, like we usually unpack a tpple. So the tpple is called singer. And we're saving the name of the singer to singer name and the song to a variable called song. And then we're using it below. But in this case, we just need the first part of the tpple. We're just looking for the name of the singer. So I'm going to access the name of the singer in the tpple using indexing. So when you're using indexing, singer zero is the name in the tpple and singer one is the song. So for every singer tpple, I'm going to want to look at singer zero. So if we look at the queue up here, the loop singer in Q is getting us into each tpple. And as we're looking at each tpple, I'm saying that I want to get at the item in the tpple at index zero. So I want to check if singer zero equals name and if we find a match I want to go ahead and remove that entire tpple from the list. So I'll remove the entire singer tpple. Then I'm going to print a message saying removed name from the que. Once the loop finds a match I want to return to completely exit out of the function. And if we don't find a match I want to print there's no one named name in the queue. Now, let's check to make sure this works. I'm going to say I want to remove someone from the list. Who do I want to remove? Let's try somebody who doesn't exist. First, I want to try to remove Ellen. It says there's no one named Ellen in the queue. Great. That's exactly what we want. Now, let's try to remove Allen. It says removed Allen from the queue. And we can see in our current queue that Allen is gone. Perfect. Excellent work. You've done an awesome job. It would be great if our karaoke queue was numbered. Then the host would know how many people are in the list and the singers would know what they're always dying to know. How many songs are ahead of theirs? So, how do you number a list that's constantly changing? I'll show you an example. I have my list of contacts here. And as you know, if I wanted to loop through them, I could say something like for contact in contacts. If I wrap contacts in a built-in function called enumerate, that's going to give me access to the index of each element as I loop through it. So I now have access to two variables, index and contact, which represents the item in the list. By convention, when we access the index, we shorten it to i. So now let me print I and contact. And when we run it, prints every item in the list and its index. Now I'm going to hand this over to you. Here in Singout, I'm going to go to show q. And here are your instructions. I want you to update show q so that the list is always numbered. That means you want to print each singer with their number starting with one. So notice when we looped, we start at zero because Python starts counting at zero, but humans start counting at one. So, you'll need to figure out how to do that. And here's a hint. You can do math inside of an F string. And then finally, try adding and removing several singers from the list to make sure that the numbering stays consistent. And when you're done, here's what you want the Q to look like. The number in the queue, their name, and the song that they're singing. Go ahead and get started. Okay. Okay, I'm going to come down here into show Q and I'm going to update this so I have access to I and Ser and I'm going to wrap Q in the enumerate function. Now I'm going to add to my F string. So at the beginning it prints I with a period at the end. Let's check this out so far. Looks like I have spelled enumerate wrong, which is bound to happen. All right, we've got Annie starting at number zero and Allen starting at one. So, how can I fix this? I'm going to come here into my f string. And this is what's called an off by one error. It's the idea that Python starts counting at zero and humans start counting at 1. So, I can fix this by saying that I want to display I + 1. So, that should correct the way the Q is numbered and quit out of this and run it again. There we go. Now, the numbering is correct. Let's try adding somebody just to make sure. Now, we have share at number three. Now let's try removing somebody. Let's remove share. So that number is correct. Even though singers are constantly being added and leaving the queue, one of a karaoke host's most important job is calling the next singer up to the mic. That singer is at the front of the queue. So we need to be able to take them off the front of the list and announce that they're the next one singing. Let me show you how we can do that. I have a list of tasks. Let's pretend that it's a to-do list. And let's say that I finished the first task on my list. So, I want to remove it. I can do that with a built-in Python function called pop. Unlike remove, which takes in a value, pop takes an index. So, if I want the first item in the list, I'm going to need to pass it zero. I'm going to have to specify which list I want to work on. So, I'm going to say tasks. And the other thing that pop does is that it returns you the item that you removed. So the item you removed is available to save to a variable. So I'll name the variable first as if I'm removing the first task on my list. So let's print the first variable. And let's also print the tasks list. That returns me the item I popped from the beginning of the list and also my list which no longer contains that item. So the difference between pop and remove is that remove takes an item out by its value and returns nothing. and pop takes an item out of a list by its position and returns you that item. So now it's your turn. Here are your instructions. I want you to write a brand new function called next singer which will take in the que. And if you have a look at the singout file, you'll see that I've marked where that function should go. So in this function, you'll want to take the singer off the front of the queue using pop and save the return value. When you use pop on our list, it's going to return the whole tpple and not just the singer's name. So, you want to unpack the singer into a name in a song and announce that they're next. And then you'll want to add next to the options menu and make sure next singer is called when a user types next. Here's our example output. Here's our Q. We have a next option added to the options menu. When the user types next, we'll see an announcement that says now up Annie who is singing dancing queen. And then when the queue reprints, we'll see that there is only one item in it. And if you have more than one item in your list, you should see that the first item is gone. Go ahead and give this challenge a shot. Over here in Singout.py, I'm going to define a function called next singer. I'll pass in the Q. Then I want to call pop on my Q and I want to remove the first item in the list at index zero. And this is a tpple. So I'm going to unpack this into name and song. Now that I have name and song saved to variables, I'm going to create an F string and it's going to say now up name dash song. Now I need to come down here to my else if chain and somewhere in here I'm going to add a new l. I'll add it after add and I'll say l if command equals next. I want to call the next singer function passing in the que. Let's see if this works. So we've got Annie and Allen in the queue. I still need to add this to my options menu. Let's go ahead and type next and see what happens. There we go. Now up. Annie singing dancing queen. And we can see that that has disappeared from our que. So that's perfect. Let's go up to the options menu and add next as an option. And also let's quit and run again. Say next. And it looks like we could use a space here. So I'm going to go ahead and add that. Add a blank space right here. And let's check this one more time. There we go. That's looking great. Let's keep going. Often a karaoke host needs to bump someone to the top of the queue. Maybe it's their birthday or they've slid the karaoke host a generous tip to move a singer to an exact spot. We can use the insert function. So I have a list of tasks here and let's say I have a task that's more urgent. So I want to put it at the front of the list rather than appending it to the end. So I can say I want to insert and insert takes two things. The desired position or index where I want to insert a value and the value itself. So I can say in the tasks list at position zero I want to insert the item pay rent. Now when we print tasks you'll see that item has been inserted. There it is. Pay rent inserted at the top of the queue. To move an item already in a list is two steps. First we need to pop it out of where it is and then insert it where we want it to go. Let's say that calling my mom becomes the most important item on my tasks list. So I can say tasks.pop pop and I want to pop from the list the item at position two. So we've inserted pay rent. So pay rent is at position zero, do laundry at one, and call mom at two. This is going to return to me the call mom task. So I'm going to save that to a variable called to-do. And now that I have call mom saved to a variable, I can go ahead and insert it back into the list. So into the task list, I want to insert at position zero the to-do list item call mom. Now let's move this print statement down here. And you'll see that the list has transformed and we've moved the call mom item to the front of the list. So now it is your turn. Your task is to write the entire move to top function. And you'll do that here in Singout.py. And here are your instructions. Moving a singer to the top of the queue is two steps. Number one, take them out of their current position. And number two, insert them at the beginning of the queue. So in your move to top function which needs to take in the queue, you'll first ask the host which position they want to move and convert their answer to a number. Then you'll take that singer out of the queue with pop. And this is important. You'll need to watch for what's called an off by one error. Our Q starts counting at one, but remember that Python starts counting at zero. So when we're looking at our Q, Annie is at position one, but in our list, she is at position zero. So you'll need to account for that in your code. And here's a hint. Pass the host number minus one as the argument to pop. Then you'll put the singer back at the front of the queue with the insert function. And finally print a message announcing who has been moved to the top. You'll also need to call move to the top in the app loop so that the host can run the command and also add it to the options menu. So here's your example output. You'll see in the options menu, we've added a top option here. When the user types top, it should say, "Who do you want to move to the top? Enter a number." The user enters something like two. Then behind the scenes, we'll move Allen up to the top of the queue and send the message moved Allen to the top of the queue. And then here's what the new queue looks like with Allen at the top. Go ahead and get started on the challenge. Okay, hopefully that went well. I'm going to go into Singout.py and start creating my move to top function. I will take in the Q and I'll start by prompting the user for a position number. I'll convert the position number to an int and save to a variable to capture their answer. I'm going to make a little room in the display by adding a blank print statement here. And now whatever position they've entered, I'm going to want to use the pop function to remove it from the que. So from the queue, I want to pop the item at the position number that I've gotten from the user. But to take care of the off by one error, I'm going to need to say pop position minus one. So again, when the user is looking at the list, they're going to see Annie as number one and Allen as number two. If they want to move Allen, they're going to say they want to move the person at position two. But for us in our list, because it starts counting at zero, this is actually going to be position one, 0 and one. So for example, if we were asking for Allen, we would be asking for position 2 minus one, which is position one. So that's how we take care of the off by one error. Now I'm going to save the item at this position to a variable, which I'll call singer. And I'll use insert to move them to the front of the queue. So into our Q list, I want to insert at position zero this singer that I've popped off the list. Now that I've successfully moved the singer, I want to give the host a message letting them know that the singer has been moved. So I'll say something like moved name to the top of the queue. And because I need the singer's name, that means I'm going to have to unpack this tpple that we get back from singer. So I'm going to unpack into name and song. And then the first item in the tpple, which I've saved with a variable name, will end up in our fstring. There's my function done. Now, I'm going to need to wire this up to the app. So, I'm going to come down here to my ifels chain. And really, I could put this anywhere I want, but here under next, I'm going to say if the command equals top, then I want to run the move to top function. And my final step will be to go into show Q and add top as an option in the menu. Let's run and confirm that everything's working. I'll type top. I want to move the singer at position two. And we have a message that says moved Allen to the top of the queue. And here's our current queue. And you can see that we have successfully moved Allen to the top. Perfect. Great job. Excellent work. Every if statement you've written checks for a single condition, but you often need to check for several conditions. Say I'm writing the rules for an amusement park ride where you have to be over a certain age. Let's say 12 or over and also a certain height to ride a ride. Let's say 140 or over. With an if statement, I can say if age is greater than or equal to 12 and their height is greater than or equal to 140, then we can print this person can ride. And we can include an else here that says sorry not this time. Now I'll create two variables. I'll say age is 70 and height is 150. Let's run this and find out if the person can ride. This person can ride. If I change the age to 11, we'll get the opposite message. So that's the and logical operator. It's going to evaluate both of these conditions. And both of these conditions must evaluate to true for it to run the code in the if block. So you can think of this as saying if true and true print this else print the second message. The or operator evaluates both of these statements and one of them must be true in order for this code to run. Age equals 11. So age greater than or equal to 12 will evaluate to false. But height is greater than or equal to 140. We'll evaluate to true because it has to be either or. If we run this, we should see this person can ride. There's also a logical operator called not. So I'll create a variable called logged in which I'll set to false. Then I can say something like if not logged in, print please log in. Not essentially flips a boolean to its opposite. Logged in is false. So not logged in evaluates to true which runs the code in the if statement. You can also pair not with a list and it checks that an item isn't in the list. So let's say I want to see if a show is sold out or not and I know that it's sold out on Tuesday and Saturday and I also know that today is Wednesday. Then I could write something like if day not in sold out print tickets are available. So day is Wednesday which is not in sold out. So when we run this we should see that tickets are available. And now it's your turn to practice using some logical operators. Here is your challenge. Your challenge is to write one if statement for each situation combining the values given with and or not. Number one, a house plant only blooms if it got enough sun and someone watered it. Print it bloomed when both are true. Number two, you'll wear a ridiculous hat to the party if it's a costume party or you don't know anyone there. Print hats going on if either is true. Number three, the cat lays across your keyboard on work days, so it wins when it is not the weekend. Print cat wins when it isn't the weekend. Go ahead and get started on the challenge. All right, for the first one, I'll say if got enough sun and was watered, we'll print it bloomed. For the second one, if is costume party or knows nobody, print hats going on. And for the third, if not is weekend, print cat wins. Let's run and make sure these work. There we go. Logical operators. Remember that and evaluates to true only when both sides of the condition are true or evaluates to true when either side of the condition is true and not flips a boolean to its opposite. This serve order function pops the first order off the list and serves it. But there's one problem. What if you call serve order on an empty list? This throws an error and as it helpfully tells us, it's because we're trying to pop from an empty list. To catch the crash before it happens, we need a way to check whether the list is empty. And we can do that with the len function. Let's say I have a list of orders with some stuff that you would find in a coffee shop. Now I can print orders and wrap the len function around it. And that will tell me how many items are in the list. An empty list has a length of zero. So we can use that to check whether a list is empty. So back in my function here, I'm going to make some space. I'm going to say if the length of orders equals zero, I'm going to print nothing left to serve and immediately return. This is called a guard clause. A guard clause lives at the top of a function to check for a bad case. And if it finds a bad case, such as a list being empty, it handles it and returns right away. So here if the list is empty we print a message and return so the function never reaches pop where it will error out and if you get past the guard you can assume that we found an item and the code can continue. So let's save and run this and see how it goes. Now instead of crashing we get the message nothing left to serve and I'll just show you that if there is an item in the list then this code goes ahead and runs. You can also stack guards to check for multiple bad states. For example, I will make a function called fill order and let's say it takes a menu and an order. So I could do something for example like check if there are any items on the menu, maybe the entire restaurant sold out. And if not, I can print nothing available and return. And then I could also write another clause. And let's say I want to make sure that an order hasn't been placed that's not on the menu. So I could say order not in menu. And if someone tries to order something that's not on the menu, I can print a message like order isn't on the menu and return. And so once I've made sure that there's food on the menu and that the order is on the menu, I can go ahead and do the regular tasks of my function. For example, I could remove an order from a menu or print that an order has been placed. Something like that. So now it is your turn. Call Next seats the first guest on a restaurant's weight list. But when the wait list is empty, the app crashes. So here's our empty weight list. And here we have a function call next that takes in weight list. And it's looking for the first name on the weight list so that it can print a message about it. And as you can see, if we call this function, it's going to crash with an index out of range error. So, what you need to do is add a guard clause to call next that exits early when the weight list is empty and prints a message instead of crashing. Then go ahead and test it with an empty weight list to confirm that it no longer crashes. Go ahead and give this a try. All right, I hope that went well for you. At the top of my function is where my guard clause goes. So, that's where I'm going to start. And I'm going to check for an empty weight list. So I'm going to check the length of the weight list and if the weight list equals zero I'm going to print the weight list is empty and I am going to return with an empty weight list it should prevent this code from ever running because the length of weight list is zero. So it will print this message and exit out of the function. Let's go ahead and give this a check. So it says we have an unexpected token weight list at line 30 and that probably means I'm missing a quotation mark. Yes, I am. Let's try again. The wait list is empty. Great job. Sing out has all its planned functionality, which is definitely worth celebrating. So, big congratulations. But there's still a little bit more work we need to do. And that's because every single function in the app assumes that there are singers in the queue and that the host's input is going to be usable. When the queue is empty or if the host enters blank input, we're going to run into some problems. Let me show you. Going to go ahead and get the app up and running. I'll type next twice to empty the queue. As you can see, show Q prints current Q with a blank space underneath it instead of a list of singers because the queue is empty. This isn't a crash, but it does look a little bit broken. Here's another problem. Let's see what happens if I type next again when the queue is already empty. That's actually going to cause a crash because we're trying to use pop on an empty list. Now, I'm going to clear and restart. Here is another issue. When I type top, it asks me who do I want to move to the top. Enter a number. What if I enter the number for a singer that doesn't exist? So, I'm going to say I want to move the singer at position 10. This is also going to cause a crash because pop can't find that index because it doesn't exist. There's no one to move. The next one I want to show you is if there's only one person in the queue, I'm going to type next to call a singer up. So now there's only one person in the queue. Now if I type top, it's going to ask me who I want to move to the top. So I'm going to type one. The app very happily claims to move that singer to the top when it should be telling me there aren't enough singers to reorder the list. I'm going to quit out of this. Clear and restart. And yet another problem is say I'm going to add a singer. I can actually just hit return and hit return again. And the app will allow me to add blanks to the queue. So there's just a dash. Not ideal. Again, this isn't exactly a crash, but it does look strange and we want to try to prevent that behavior. The final one I want to show you is a problem with the remove command. I'm going to use next to empty the queue. And then I'm going to type remove. And it's going to ask me who I want to remove. Let's say I want to remove Shelly. And it's going to tell me that there's no one named Shelly in the queue, which is technically true. But if the queue is empty, we don't want the user to have to go through all the rigomearroll of typing a name to remove only to be told that there's no one by that name in the queue. Instead, it should just tell me that the que is empty. To fix all of this, we're going to need to use some guard clauses. Remember, a guard clause is a check at the top of a function that catches bad input and returns it before we get to the lines of code that put the app at risk for crashing. I'm going to make this window a little bit smaller and I'm going to demonstrate using the next singer function. The next singer function pops the first singer off the queue and then displays the now up message. So, what I want to do here is add a guard clause at the very beginning that's going to make sure that the Q is not empty before it moves on to this code. So, I'm going to check the length of the Q. And if the length of the Q equals zero, then I'm going to print a message saying oops, there's no one left to call up. And then I'm going to go ahead and return. So, all that will happen if the que is empty is it will print this. And then the function will exit. Then unless we quit out of the app, it's going to trigger show q to run again with our command prompt so that they can try again with a new command. So your challenge is to add the rest of the guard clauses that we need. So under each function that needs a guard clause, I have included some instructions as code comments. Let's go through them really quick. For show Q, when the Q is empty, the user sees the current Q header with nothing under it. This one is a little bit unique because your guard clause will use an if else. If the Q is empty, you'll print a message saying that the Q is empty or else you'll want to redraw the que. Here I have an example output file so you can see what is supposed to happen once your guard clauses are in. So in show Q, when the Q is empty, the message should be the Q is currently empty followed by the command menu. We don't need to do anything for prompt for singers. So, we'll skip that one. For add singer, you'll need to write one guard clause that stops the function and prints a warning if the host has entered a blank name or song. And here's a hint. If name or song are equal to blank strings, then you'll know that you need to print a warning and exit out of the function. We've already done next singer, but let's go ahead and check it really quick. I'm going to quit out of this and run again. Let's do next and keep doing next until we are out of singers. And there we see no crash. Instead, we see a message that says, "Oops, there's no one left to call up." So that's great. Now, let's look at the move to top function. For the first one, and this is where it should go. You need at least two singers to reorder the queue. So, warn and stop if there are fewer. So, for this guard clause, you'll need to check the length of the queue and make sure there are at least two singers. and if not, print a message and exit out of the function. And here's what that should look like. You attempt to move a singer to the top of the queue when there are fewer than two singers. The user types top and you print the message, you need at least two singers in the queue to move someone to the top. Then after you get the position of the person in the queue that the user wants to move, you'll need to add a second guard clause. Entering an invalid position crashes the app. Make sure the position the user enters isn't out of range. And that means it can't be less than one because the position of the first person in the list is one and it can't be greater than the length of the list. If we have a list with five people, then nobody exists at position six. And finally, we need a guard clause for the remove singer function. So, as we saw, this function runs normally even if the queue is empty and there's no one to remove. So you'll want to write a guard clause that warns the user and stops the function if the queue is empty. This is very similar to what we did in the next singer function. So here's the example output for that. The user types remove on an empty Q. You'll print a message that says oops, there's no one left to remove. And then this comes from show q already. The que is currently empty. Plus the command menu reprints. So go ahead and go down these list of functions one by one. Make sure that you look at the expected output for each condition. I'm confident that you can do it. But if you get stuck, don't worry because you'll be able to look at my solution. Go ahead and get started writing guard clauses. Okay, let's start by writing guard clauses for show Q. We want to make sure to print a message if the Q is empty. So I'm going to say if the length of the Q equals zero, we'll print a message that says the Q is currently empty. Else we want to redraw or display the Q normally. So I put my else here. And now I'm going to indent the rest of the code to put it inside of the else. So in other words, if the Q is empty, print this. And if it's not empty, print this. The next one is add singer. So I need to guard against entering a blank name or song. So after I've prompted for a name in a song, I'm going to say if name is an empty string or song is an empty string. I'm going to print a warning message and return. So we'll get the name and the song. We'll make sure it's not blank. If it is blank, we'll go ahead and exit out of the function. And if it's not blank, Python will move on to these lines of code. Let's check our work so far. I'm going to quit to exit out and clear. And let's rerun. Let's hit next a couple of times to make the queue empty. Now we can see we get a message. The que is currently empty. That's great. Now let's try adding somebody. We added somebody back to the queue. Now let's see what happens if we add a blank name and song. Instead of adding it to the list, it says, "Oops, I need a name or a song to add someone to the queue." So that's exactly what we're looking for. Let's keep going with these guard clauses. The next one is move to top. And we need two guard clauses here. First, we need at least two singers to reorder the que and to warn or stop if there are fewer than two singers. So, I'm going to check the length of the Q again, but this time I'm going to say if the Q is less than or equal to 1. So, that guards against an empty Q and a Q with only one person in it. So if we have zero or one people in the queue, I'm going to print a warning and return to exit the function. The second one comes after we have asked for an input, which is the position of the person they want to move to the top. So just as a reminder, if I were to rerun the app and I say top and then I enter 99, then it's going to crash out because there's nobody at position 99. So, I need a condition that's going to check and make sure that the number is not out of range. So, I have my position number that I've got from the user. I need to make sure that position is greater than one. So, I'm going to say if the position is less than one or the position is greater than the length of the Q, then I need to print a message and return. So, in other words, if the position is zero or a negative number, or if the position is longer than the Q itself, let's print a warning message. There's no singer at position number. Let's check both of these. I'm going to hit next. So, the queue only has one person in it. And then I'm going to attempt to move somebody to the top. And it's going to tell me you need at least two singers in the queue to move someone to the top. So, that saves the user a little bit of time. That's great. Now, let's add someone back to the queue and try to move a position that doesn't exist to the top of the queue. So, I'm going to try 10. And instead of crashing, we get a message that says there's no singer at position 10. We've addressed a big range of errors and other things that could confuse our user, the karaoke host. If you want to go even further, try looking for other gaps in the app. Here's an example. If I try to remove someone who isn't in the queue, that's covered. It says there's no one named Rachel in the queue. But I can also hit remove and then hit enter again with the blank and it's going to say there's no one named blank in the queue. So that's another example of a guard clause you could write if you're looking for more practice. The move to top function in the Singout app ask the host for a position number, but there's nothing stopping the host from typing a word instead. I have no problems converting some types of strings into an integer. Let's say I have a number and I have a string 42. I can very easily convert that to a number by wrapping it in int. And if I print the number, that's going to go just fine. But if I try to wrap a word like banana in the int function, int's not going to know what to do with that and it's going to crash. So, int can convert a number that is a string into the Python data type number, but it can't convert a word to a number. It raises this value error instead. To keep the app from crashing and give us an opportunity to address this, we can use what's called a try except. So, I can say try converting this value into an integer. And if the result is a value error or if the exception is a value error, then I can print something like please enter a number. So it's going to try converting banana to a number and if it can't do it, it's going to fall to this accept block and run this code instead. And if we were running an app like the Singout app that runs continuously until the user quits out, this would have caused the app to crash and quit out. So this is a way to prevent bad input from crashing an app. You're almost always going to use try except around an input. So for example, here inside the try block, let's say I want to prompt the user for an age. If I'm able to successfully convert this to an int, then I can print something like you'll be age + 1 next year. And if I get this value error because int can't convert whatever the user inputs to a number then we'll print please enter a number. So I get how old are you? I'm going to print 20 and it says you'll be 21 next year. Let's try this again. And we'll type 55. And then we get the output. Please enter a number. So now it's your turn to use try except with a little challenge. You're building a sign up for a hot dog eating contest. entrance type how many hot dogs they think they can eat, but nothing stops them from typing something that isn't a number. So, step one will be to ask the entrant how many hot dogs they can eat and convert that number to an int. Step two, if the number that you get back isn't a whole number, print I need a number to sign you up. And step three, if the answer is a whole number, print signed up for however many numbers of hot dogs. Good luck. Here's what your output should look like. You ask how many hot dogs can you eat? They put in a number which is seven. You convert that to an integer because if you'll remember inputs always come back as strings. So you convert that to an integer and you're able to print signed up for seven hot dogs. Good luck. And here's an example of the bad output. You ask them how many hot dogs can you eat? And they type in something like a lot. In which case you will print a message that says I need a number to sign you up. Go ahead and get started on the challenge. First, I'm going to try prompting the user for how many hot dogs they can eat. I'm going to save the result to a variable called count. If I'm successful at getting a number, I'm going to print a message telling them how many hot dogs they signed up to eat. And if a value error exception is thrown, we're going to print I need a number to sign you up. I'm going to comment out the previous code and let's give this a try. Looks like I ended up with an extra quote. So, I'll fix that. Try this again. I'm going to say 25. Signed up for 25 hot dogs. Good luck. Now, let's try with a non-number. And that's going to come back with I need a number to sign you up. So, this is an example of how you can use try and accept. If this int call fails, Python will jump straight down to what is inside this accept block, which allows you to determine what's going to happen if you run across a bad value. Instead of your user encountering a strange error that they might have to parse or giving them a userfriendly message instead. We are so close to wrapping this up and calling the karaoke queue manager. Finished. There's one last crash that we're going to address. So, I'm going to run the app. I'm going to type top to move somebody to the top of the queue. And let's say instead of a number, I accidentally type Allen. Yes, that is going to cause the app to crash. As you can see, it says we get a value error. Invalid literal for int with base 10. Allen. That's a fancy way of saying I have no idea how to convert the word Allen into a number. I'm going to leave fixing this up to you. Here are your instructions. When trying to move a singer to the top of the queue, the app will crash if the user enters input that can't be converted to an integer. Use try except to handle the error. Here is your expected output. The app asks, "Who do you want to move to the top? Enter a number." The user enters a word like two that can't be converted to an integer and the app instead of crashing will say please enter a number. If they enter valid input, so enter a number and they enter the number two, it will say moved Allen to the top of the queue. You have already written the code that does this. So you just need to confirm it. You don't need to write it. I've marked where you should validate your input right here. So go ahead and get started. I'm going to move this user prompt inside of a try block. So, we're going to try to prompt the user for a number and convert it to an int. If we can't do it, we're going to catch that value error. I'll add a blank line and then we're going to print please enter a number. Then, we need to return out of this function which will trigger the command menu to show again so that they can enter another prompt or try again. Let's confirm that this works. So, I'm going to type top. It says enter a number. I'm going to put Allen. And it says, "Please enter a number." And then I get my Q again. And my options menu again. Just the behavior that we're looking for. Let's confirm that top still works as expected. So, I want to say move the person at position two to the top. And that has moved Allen to the top. Perfect. Really fantastic work. This app is done. Congratulations, you've built Sing Out, a complete karaoke queue manager. The host can add singers, call the next singer up, move a singer to the top of the queue, and remove people from the queue. You've also made it robust. Rather than crashing on user input that it doesn't expect, your app gives the user an informative message and keeps running. You've covered a lot to get here. You plan the app before writing it, naming the tasks, stubbing out the functions, and documenting using dock strings. You practice separation of concerns by keeping each task in its own function. You wrote functions that take arguments and return values, looped over lists of tpples, and used append, remove, pop, and insert to manage the queue. You combine conditions with logical operators, and you harden every function with guard clauses and try except. Absolutely amazing work you did here. And you can still go further with this. For example, you could add a clear command that empties the queue or a swapped command that trades two singers position rather than just moving them to the top. This is a big stop on your Python journey, so you should be very proud. I'll see you in the next one and happy coding.
Generated algorithmically for Search Engine
Indexing.