Learn Python With This ONE Project!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this video i'm gonna teach you python by working through a project now this project i'm gonna write completely from scratch which means i don't have it up on my other screen i've not ridden this before it's a completely new project to me now the point of this is not only am i going to teach you basic python syntax and some different language features i'm also going to explain to you how you structure a program how you've decide where you're going to start what different components you need to build out and kind of how you go about the different tasks this is going to be perfect for people that have a little bit of experience with python they've written some code before but they're not yet comfortable and confident to go and write their own project where they don't know where to start when they're kind of diving into a task on their own hopefully this is going to make you a lot more confident you're going to learn a bunch about python and you're going to have a good project that you can show after this video so i encourage you to follow along type this project out with me with that said let's dive in after a quick word from our spots here before we get started i need to thank octo ml for sponsoring this video as we know building and training the perfect deep learning model for your application is just the first step after that it's time to push it to production now rather than learning completely new tools for model acceleration and containerization you need a bridge that gets you from model building and training to deployment as fast as possible now that's where octoml cli comes in using octo ml is very similar to working with docker and kubernetes you start with a trained model and ingest it into octo ml next octo ml packages the model into a docker container with nvidia's trident inference server which you can deploy to your local docker desktop any cloud kubernetes service like amazon eks azure aks or even managed ml services like amazon sage maker when you're ready you can then use octo ml to accelerate your model peak performance on your cpu or gpu hardware target get started today by clicking the link in the description and downloading the octo ml cli for free make sure to be one of the first 20 tech with tim subscribers to try octo ml to receive a special welcome gift so you're probably wondering now what kind of project are we going to work on now i wanted to pick something that was unique and that i haven't done before so we're actually going to build a text based slot machine now the way this will work is the user will deposit a certain amount of money we're then going to allow them to bet on either one two or three lines of the slot machine just to keep it pretty simple i know in real slot machines they have a lot more lines than that and then we are going to determine if they want so if they got any lines we're gonna multiply their bet by the value of the line add that to their balance and then allow them to keep playing until they want to cash out or until they run out of money so this is actually a fairly complex project because we need to do a lot of things we need to collect the user's deposit we need to add that to their balance we need to allow them to bet on a line or on multiple lines we then need to see if they actually got any of those lines we then need to spin the slot machine or we would have done that before right we need to generate the different items that are going to be in the slot machine on the different reels and then we need to add whatever they want back to their balance so there's quite a bit of stuff going on here and this should be a challenging enough project to make it interesting for you guys so before we even get into this i just want to mention i obviously am not supporting gambling here this is just kind of a cool thing that we can do we're not using any real money and i think it's a good project so just keep that in mind do not gamble i'm not supporting that by making this video and before i write any code what i like to do here is just think of the different things that we need to do in this project and kind of pick a good starting point the place where i like to start usually is collecting some user input and for this project we need to get the deposit that the user is entering as well as their bet right those are kind of the two things we need before we can start actually using the slot machine and generating you know whatever the slot machine spun so to do this i'm going to make a function here called deposit now i'm just inside of visual studio code you can work in any any editor you want sorry and i have this main.pie function open i assume you guys can open your own python file and kind of work in your preferred environment so this function here is going to be responsible for collecting user input that gets the deposit from the user now a function if you're unfamiliar is just something that we can call that's going to execute a certain block of code and then can potentially return us a value and you'll see how this works in a second so what i'm going to do here set up a while loop and the reason i need a while loop is because i'm going to continually ask the user to enter a deposit amount until they give me a valid amount so if they don't give me a valid amount then they need to keep typing in until eventually we get one so to do this i'm going to say my amount is equal to input and then inside of input i'm going to pass a prompt which is some text that will happen before we allow the user to start typing so for the prompt i can say what would you like to deposit question mark and then i can put something like a dollar sign and then allow them to start typing after the dollar sign okay now what i need to do is check if this amount is actually a number because they could type in anything here right they could type in hello world they could type in nothing and just hit enter they can do whatever they want so i need to make sure that the amount actually is a number before i get out of this while loop so i'm going to say if the amount dot is and then digit like this then we can do something inside of here now is digit is just going to tell us if this is a valid whole number so if it's something like a thousand you know 10 whatever if they type in a negative number it actually won't be true so if they do something like negative 9 then it's not going to be true and we call is digit anyways this is a method you can use on strings to determine if this is a valid number so if this is a digit then what i'm going to do is i'm going to convert this into an init so i'm going to say amount is equal to int amount and that's because by default this comes as a string and we want to have a numeric value for our balance or for our deposit so we need to convert this to an it however i can't do this before i make this check because if i do that then this could potentially fail because if they try to type in something like hello world then this here is invalid i can't convert hello world into an integer so that's why i'm first checking if they did actually enter a valid number if they did i then convert it to a number now what i need to do is check if this number is greater than zero so i'm going to say if the amount is greater than not equal to but is greater than zero then i'm going to break out of this wall if it's not greater than zero then i'm going to put an l statement here and i'm going to say print amount must be greater than zero dot okay there we go now i'm just gonna add one more else statement down here and i'm gonna say print uh please if we could type this correctly enter a number okay then finally here we are going to return our amount all right so let me go through what we just did here so we wrote a function deposit this is a while loop so we're going to continue to do this until eventually we break out and this break keyword just ends the while loop and then we'll bring us down to this line i ask what is the amount you want to deposit they enter something if this is a digit then i'm going to convert this to an integer and i'm going to check if this is greater than zero if it is it's a valid amount we can work with this we'll break out and then we'll return that amount and we can use that later on okay otherwise i'm going to print this amount must be greater than zero because if it's not greater than zero well we need to tell them that then get them to try again all right so that handled this first if statement now if the amount is not a digit then what we're going to do is uh print sorry please enter a number and we're going to continue this until we get a number and until we break all right so that's that function now to call the function if we spell uh deposit correctly here i don't even know how i messed that up so badly okay deposit uh to call the function we simply write the name of the function and we put our two parentheses and now as soon as i run my code this function will run and we'll be able to actually enter a number so let's do this you can see in my terminal that it's asking me what would you like to deposit let's go with something like ten dollars and all is good now if i run this again and i enter something like hello world it tells me please enter a number okay let's enter zero all right amount must be greater than zero enter one i'm good to go so there we go we've written our first deposit function and i've tested it it's working good we're all good to go all right so now what i'm going to do is i'm going to say my amount is equal to my deposit and actually we're going to call this balance and this is kind of the start of our program here and in fact i'm going to put our program in this function main so that if we end our program we can just call this function again and then it will rerun the program right so i can say do you want to play again and then we can rerun the main function and we'll be good to go so we've done that now i need to call the main function so that we start running main and then it's going to do everything inside of here okay so now inside of made the next thing that i want to do is i want to collect the bet from the user so there's actually a few ways i can do this but i need to determine how much they want to bet and then how many lines they want to bet on and then i would multiply their bet amount by the number of lines so it probably makes sense to ask the number of lines first and then ask them how much they want to bet on each line so it's not confusing if i say how much do you want to bet they put ten dollars and then they say three lines and ends up being thirty dollars see what i mean so let's say define and we'll say get number of lines like this and inside of here we're going to ask them to pick a number between 1 and 3 because that'll be the number of lines that we have and to keep this program nice and dynamic i'm going to add what's known as a global constant at the top of my program and i'm going to say max underscore lines is equal to 3. now i'm doing this in all capitals because this is a constant value something that's not going to change and this is a convention in python you do it in all capitals and i'm making it equal to three okay so now anywhere in my program where i'm referencing the number of maximum lines in my slot machine rather than writing three i'll just write this and then later on i can easily change this to be five and the whole program will update based on that right so let's make this three for now okay so get number of lines obviously the minimum lines will be one we don't need to write that in okay so we need to ask them to enter this so we can actually just copy everything we have in the deposit function and just change a few values here so we're going to say while true rather than amount we're going to say lines and we'll say enter the number of lines to bet on okay and then i want to put inside of here kind of the option so like one to three so i'm going to write 1 dash and then i'm actually going to do a concatenation here plus and then i'm going to convert my number of lines or my max number of lines to a string and then i'm going to say plus and then i'm going to put a ending bracket here question mark and then a space okay i know seems a little bit confusing but what i've just done here is i have now added max lines inside of this string there's a few other ways to do this but this is the most basic so that's how i'm doing it and i need to convert this to a string because if i add two strings together they get squished together but if this was a number and i tried to add it to the string then this would cause an exception in my program so enter the number of lines to bet on one dash and then i'm going to put in whatever the string version is of the maximum number of lines end my bracket put a question mark and then i'm putting a space so that when they start typing they're not like connected to the question mark they're off by one space and it looks easier for them to read right okay so now same thing i need to make sure that they actually did enter a number so i'm going to say if lines dot is digit then i'm going to say that my lines is equal to int lines like this and i'm going to check now if the lines is within the bound that i had so within 1 and 3. so the way to do this is i'm going to say if and i'm actually going to say 0 actually we'll say 1 less than or equal to lines less than or equal to and then this will be the max lines all right so this is something you can do in python you may not have seen before if i want to check if a value is in between two values i can write it like this so what i'm saying here is if my lines is greater than or equal to one and is less than or equal to the maximum lines then i'm okay i can break otherwise i need to tell them to enter a valid number of lines so i'm going to say enter a valid number of lines okay otherwise same thing we'll say enter a number and then rather than returning the amount we'll return the lines okay perfect so now we have a function that gets the deposit amount and the number of lines so now in our main function we'll say lines is equal to get number of lines like that and now what we can do is print out the balance and the lines just so we can see what this is when we run the program okay so now let's go here let's run and how much would you like to deposit let's go with a hundred dollars okay enter the number of lines let's try four okay it doesn't work let's try zero doesn't work let's try h e doesn't work let's try two valid and notice i now have a balance of 100 and the number of lines is 2. the next thing i need to get for my user input is the amount that i want to bet on each line so let's do this and we'll have a maximum bet as well so we'll say max bet is equal to let's go with something like 100 and let's say the minimum bet is equal to one dollar and again we're putting these as constants so that we can use them kind of anywhere in our program so now let's just write this function from scratch and say define get bet so we can add our colon and continue and forget bet we'll just ask them how much they want to bet uh and honestly actually now that i think of it let's just copy the same thing from deposit i don't like repeating all of this code but they are slightly different so it's fine to do something like this so for the amount we can actually use the same value and rather than what would you like to deposit we'll say what would you like to bet question mark again we're going to check if the amount is a digit we're going to convert this now to an integer and now we need to check if the amount is between the minimum and the maximum bet so i'm going to say the min bet less than or equal to the amount less than or equal to the max bet okay and then break and here i'm going to say the amount not must be greater than zero amount must be between and now i'm going to show you a second way that we can actually kind of put variables in our string so i'm going to use an f string here only available in python 3.6 and above i'm going to say amount must be between i'm going to put my sorry squiggly brackets like this and i'm going to say min bet and then max bet so this is actually a very easy way to embed values inside of your strings you put f before the string and then you put your curly braces like this and inside of the curly braces you can write any variable and it will automatically be converted to a string for you if it can be converted so in this case i don't need to convert min bet to a string or max bet it's automatically going to get converted by python and i've just put my dollar signs here to make it look a little bit better when i say you know between the mid and the maximum bet okay uh otherwise please enter a number that's fine and then return amount okay perfect so now i'm going to say bet is equal to and then this is going to be the get underscore effect all right uh now we should actually make this a bit more clear what would you like to bet on each line okay so now that we've done this and we've gotten the deposit gone the number of lines and gotten the bet we probably want to print out here in our main function kind of what they've said so far right so we'll say you are betting you know five dollars on three lines your total bet is 15 something like that right so we're going to say print you are betting and let's do an f string here as well and then let's do a dollar sign and we're going to say whatever the bet is we're going to say you are betting whatever the bet amount is on and then this is going to be lines like this and then we'll write lines and we'll say total bet is equal to and then we'll put a dollar sign and we're going to put inside of here a variable i'm going to say total bet is equal to bet multiplied by the lines okay and we'll say this is the total bet all right so let's run this now and let's see what we get we no longer need to print out the balance in the lines by the way all right so let's run this how much would you like to deposit let's say a hundred dollars enter the number of lines to bet on let's go with two would you like to bet how much would you like to bet on each line or what would you like to bet on each line let's go with ten dollars and now it says our total bet is 20 great so this is okay but i just realized that we actually need to check if the amount that they're betting is within their balance because they can't bet more than whatever their current balance is so we need to check that and we could check this in a few different places but since i've called this get bet i'm not going to put it inside of here i'm just going to do the check here and then i'm going to recall get bet if they entered an invalid bat so i'm actually going to put now this inside of a wallet but i'm going to say well true i'm going to say bet is equal to get bat and i'm going to then say total bet like this inside of here so we put total bit okay now i'm going to say if my total bet is greater than my balance then we'll say print you do not have enough to bet that amount and then we'll say what their current balance is your current balance is and then we'll put dollar sign we're going to put our f string here and this is going to be their balance all right and then otherwise we will simply break out okay so hopefully that makes sense i also could just put a condition here for the wallop but i think this is okay for right now all right so let's try this now let's run our code how much would you like to deposit okay a hundred dollars uh let's bet on three lines let's try to bet forty dollars and says you do not have enough to bet that amount your current balance is a hundred dollars okay uh how much you like to bet on each line let's go with twenty dollars you're betting twenty dollars on three lines the bet is equal to sixty dollars okay so we have now successfully got the number of lines the betting amount b deposited about now what we need to do is we need to actually run the slot machine now this is where it gets a little bit more complicated and i'm going to start importing some modules so the first module i'm going to import is the random module because we need to generate um the slot machine values kind of randomly right so how are we going to do this well the first thing we need to figure out is how many items we want to have in each reel and how long we want the lines to be now slot machines can get a bit complicated in terms of how the lines work i'm going to keep this really simple and we're gonna imagine that we have a three by three slot machine and that you only get a line if you get three in a row okay if you had three in a row then you win this might not be the most balanced slot machine it might not be one you want to play on but for this project is fine so again i'm going to set some values here that specify the number of rows and columns we're going to have in our slot machine so i'm going to say rows is equal to 3 and calls is equal to 3 and if we wanted to make this a little bit better we could say i guess actually you know what row and call is fine for right now i don't know exactly what they would call uh like i guess it'd be like real count and number of reels or something like that for now though this is fine all right now what we need to specify is how many symbols are in each of our reals now it should be the same at least from what i know it should be the same number of symbols in every single real we're not doing anything really complicated when i say real i'm talking about kind of one column right so how many symbols are in that column because we're going to have to randomly select out of those symbols and then we need values for our different symbols so we need to pick kind of first of all how many symbols do we want to have in total and what do we want those symbols to be now to keep this easy we can do something just like you know a b c d like those are probably fine as the symbols um yeah we can do something like that so let's say symbol underscore count is equal to and let's make a dictionary here now for our dictionary i'm going to have the symbol be a string and i'm going to have the count of this symbol in each reel so i guess what we can have is characters that are like at the beginning of the alphabet like a be the most valuable so maybe we only have i don't know something like two a's in every single reel and then for b's we could have something like four of those for c's we could have six and for d's we could have eight now again i don't think this is going to be very balanced slot machine i'm not going for the best odds here i'm just trying to kind of make something work so let's see if this actually works for us if every single reel we have two a's four b's six c's and um what do you call it eight ds to choose from now the thing that i think is going to happen here is we're going to get a lot of situations where it's just d's that are being in the reels but let's see if this works at all in terms of randomly selecting okay so that's what we've done here now what we need is something that's essentially going to generate what the outcome of the slot machine was using these values here so to do this i'm going to say define and we'll say get underscore slot machine underscore spin okay and inside of here what we're going to take is we're going to take the rows calls and symbols and this will be the symbols that we pass so these are three parameters that we're going to pass to this function and then inside of here we can use these parameters so inside of this function again what we need to do is generate what symbols are going to be in each column based on the frequency of symbols that we have here so we essentially need to randomly pick the number of rows inside of each column so if we have three rows we need to pick three symbols that go inside of each of the columns that we have and for each column we're doing kind of a new random pick right a new random generation of the symbols now this can be a bit complicated now the easiest way to randomly select um values here for each of our columns is going to be to create a list that contains all of the different values we possibly could select from and then to randomly choose three of those values and when we choose a value we'll remove it from the list and then we'll choose again now what i'm going to do here is not going to be the most efficient algorithm but since we're dealing with small values this is fine so let's see how we work with this okay so what we're going to define here is all underscore symbols okay now this is going to be a list and what we're going to do is write a for loop that's going to add however many symbols we have here into the all symbols list so i'm going to say 4 and since we're iterating through a dictionary i can do the following i can say 4 symbol comma symbol underscore count and then this is going to be in symbols dot items now when you use dot items what this does is give you both the key and the value associated with a dictionary so i can get the key here and the value and i can just use both of them rather than looping through the dictionary only getting the keys and having to manually reference the values okay so now that i have the symbol and the symbol count i want to add this many symbols to the um what do you call it symbols list now there's a few different ways to go about doing this i think the easiest way to do this is going to be to run another for loop so that i don't confuse anyone so we're going to say 4 and then this is going to be let's do this i in range and then symbol underscore count and actually we don't even need i we're going to put underscore now this is an anonymous variable in python so whenever you need to say loop through something but you don't actually care about the count or the iteration value then you just put an underscore and then you don't have an unused variable anymore so i'm going to say for underscore and range symbol count i'm gonna say all underscore symbols dot and then this is gonna be append and i'm going to append whatever the symbol is so what's gonna happen here is i'm gonna loop through this dictionary let's imagine on the first key value pair my symbol is going to be a and my symbol count is going to be 2. all right so then i have another for loop inside of here where i'm looping through the symbol count so the symbol count is 2 and what i'm doing is doing this two times so i'm going to add this symbol twice into my all symbols list all right now that we have the all symbols list we need to select what values are going to go in every single column so how do we do this well let's make a for loop that is going to do this for every column so i'm going to say my columns is equal to a list and inside of here i'm going to place a bunch of lists which are going to contain all of the values inside of my columns now this may seem a little weird to any of you that have used a nested list before because typically when you write a nested list you kind of have all of the interior lists here that are representing your rows so if i had like 0 0 then these this would be the values that are in row 0 right or in the first row and then this would be the second row and etc however here we're doing it the other way around where each of these nested lists is going to represent the values in our column so keep that in mind i'll explain how this works in case any of you are a bit confused later on but i just want you to know that we're storing the columns not the rows inside of here okay so we have all of the symbols and now for each of the columns that we have uh we need to generate what is it the values inside of the columns and how many values do we need to generate well however many rows we have that's how many values we need so we're going to say 4 and this is going to be call in range and then calls like that and then we need to say 4 and this will be row in range rows okay so for every column we need to generate a certain number of symbols so inside of here i'm going to say that my column is equal to an empty list this is actually not going to have our columns inside of it i'm just was putting that there for an example and now i need to select a certain number number of values sorry from our all symbols list so let's see how we do this so we're going to say value is equal to random dot choice and i'm going to choose from all symbols now i can use random because i imported random here now what we're actually going to do though is we're not going to use the all symbols list we're going to use a copy of this and you'll see why but what we need to do is once we pick a value we need to remove it from this list so we can't choose that value again right so if there's only two a's we shouldn't be able to select three a's we should only be able to select at most two so if we select 1a we need to remove it so then the next selection doesn't have that as a chance so that means we need to make a copy of this all symbols list because if i start removing from this all symbols list then when i try to do the next column it's going to have values removed so that's what we need to do we need to make a copy so i'm going to say current underscore symbols is equal to all underscore symbols and the way you copy a list is you do this you put a colon here this operator here is referred to as the slice operator because if i just did this and made it equal to all symbols what happens is current symbols stores the same object as all symbols now that means anything i do to all symbols affects current symbols and anything i do to current symbols affects all symbols so this is not what we want we don't want what's referred to as a reference we want a copy so the way you do the copy is you put a colon here make sure you add this otherwise it's not going to work okay continuing here now what we're going to do is select from our current symbols and then we're going to remove whatever this value is from our current symbols list so we're going to set current underscore symbols dot remove and then value now when you do dot remove it's just going to find the first instance of this value in the list and get rid of it okay now that we've done that we want to add this value to our column so we're going to say column dot push and sorry not push i'm not in javascript right now i'm in python dot append the value all right and i just realized here we don't actually need the column and we don't need the row so we can just put underscores there now what we're going to do after this is we're going to say columns dot append and we are going to append our current column now let me run through this because i understand it's a little bit confusing we start by defining our columns list then we are going to generate a column for every single column that we have so if we have three columns we need to do everything inside of here three times that's why we have this first for loop then inside of here what all this code is doing is it's picking random values for each i guess row in our column right for each value that we're going to have so we say column is equal to an empty list we see our current symbols which are the ones we can currently select from is equal to a copy of all symbols then we loop through the number of values that we need to generate which is equal to the number of rows that we have in our slot machine then we say the first value we're going to get here or a value we're picking is random not choice current symbols this picks a random value from this list okay we then say current symbols dot remove the value so we don't pick it again and then we add the value to our column okay once all of that's done so this four loop is finished we now should have however many rows there are symbols inside of our column we now add our column to our columns list then finally we can go here and we can return our columns okay and remember that when we're looking at this list here every interior list gives us the value of the items inside of our column i know a little bit confusing this was quite a bit of logic especially if you're a beginner programmer but i told you i wanted to make this challenging enough to be interesting for you and show you some new stuff so don't worry if this doesn't make complete entire sense maybe read through this a few times pause the video go back and listen to the explanation again but this is you know how we generate the items that are going to be in our slot machine now that we have this we want a way to print this out we want to look at this because i can't even really test this yet until i can print out what's inside of all of my columns and i want to print this in like a nice way so i'm going to make a function here and say define print underscore slot machine and what we're going to take here is our columns now when we have our columns it's not really in a format that's easy for us to print because we have all of our columns kind of laid out as rows almost right like we have maybe actually this isn't going to be good but we would have like a b c and one maybe we have like a a a and again these are our columns not our rows so what i need to do is kind of flip these around so rather than having like a b c a a it would go a b c right in the first column and then this would say a a and then a like that i know that i kind of butchered this example the way that i wrote it out we need to change it from being this way to be this way right that's how we need to print it out so how are we going to do that well this operation is actually referred to as transposing because we have what's known as a matrix we don't need to get into all the fancy words but understand this is known as transposing so the way we're going to do this is we're going to write a for loop and we're going to say 4 and then this is going to be row in range and then we need to determine the number of rows that we have based on our columns now the number of rows that we have is the number of elements in each of our columns right so that's the number of vertical spaces we have so we need to look at a column and get the length of that so we're going to say the length of columns 0. now this assumes that we have at least one column and we should always have one column so it's fine to put this here but understand that if we did pass something that had no columns this would crash because there'd be no column at index 0 to access so i'm saying four row in range the line of columns 0. then what i'm going to do is i'm going to loop through all of my columns and only print the first value in it or whatever the index of my current row is again i'll explain this a second i know this is a bit confusing so i'm going to say 4 and this is going to be column in and then columns like that and when i do this now i'm looping through all of the items inside of columns so it's giving me every individual column so now that i have a column i'm just going to print the value that's at the first row of that column so i'm going to say print and then i'm going to print column at row and i'm just going to put a comma here and put a pipe operator and the reason i'm going to put a pipe operator is so we have some separation between the different items right now i just want to make sure though that i only put this pipe operator here if we're not printing the last column because if we're printing the last column then we don't want to have the pipe like off right we only want to have two pipes in the middle not one at the very end so the way i need to check this i need to say 4i comma column in enumerate columns now when you enumerate what this does is give you the index so 0 1 2 3 as you loop through as well as the item so now that i have i what i can actually do here is i can say there's a few ways to do this let's actually go back to this we'll say if i does not equal and then this is going to be the len of columns minus one we'll do this otherwise we'll print the same thing we just won't print the pipe now there's a bunch of different ways we could have done this but this is just the way that i think is going to be easiest so that's how we'll do it so the reason i'm checking if i is not equal to len of columns minus one is because the length of columns minus one is the maximum index we have to access an element in the columns list right if we have a columns list with three items the length is three the maximum index is 2. so if i is not equal to the maximum index print the pipe otherwise don't print the pipe that's how it's going to work okay so now we have print slot machine and get slot machine spin that's all this is all we need for printing the slot machine so we loop through every single row that we have for every single row we loop through every column and for every column we only print the current row that we're on so we're going to print um what is it row zero so all of the elements in row zero first then row one then row two and this essentially transposes or flips our columns from being this way to be vertical this way all right so let's try this now and then we'll kind of finish the program because we're actually getting quite close though all right so once we determine what they're betting down here what we need to do is generate the slot machine so we're going to say slot we'll save just slots is equal to and then this is what do we call this function we called it get slot machine spin okay so get underscore slot machine spin and then it auto filled this for me we're passing the rows columns and symbols so rows is all capitals actually calls is like this and the symbols i believe we called this the symbols count or we call it symbol count so let's go here and make that symbol count okay so now we should have all of the columns in our slot spin right that's what that did for us now what we need to do is we need to print this so we're going to say print slot machine and we're going to pass to this our slots and really this is the columns but i'm just calling them slots because each one is like what's in the slot right okay let's give this a shot guys there probably will be an error because that's usually what happens when you write this much code from scratch but let's see if this works okay so we are depositing let's deposit a hundred dollars enter the number of lines to bet on uh let's bet on two and then how much we like to bet on each line let's do ten dollars okay now already we got a bit of an error here because i forgot to do something which i'll add in a second but if i scroll here you can see that what's happening is we're printing everything correctly we're just not printing it on the same line which is what we need to do so ideally we want d a and d on the same line c b and c on the same line and b c and a on the same line so the way that we fix that my apologies here guys is we go back to print slot machine and here we add this thing called end equals and we just make this a um what do you call an empty string and in fact let's actually do this end is equal to a pipe with spaces all right so what end does is it tells the print statement what to end the line with now by default end is equal to what's known as the new line character or the return character or whatever you want to call this carriage return there's different names for it i call it the new line character now backslash n if you ever print this out it's what tells the console to go to the next line so we don't want to print that because if we print that at the end then that means that we move to the next line after every single row we only want to do this sorry after every single column we only want to do this after every single row so i'm going to change this to be a pipe so now we'll just print this pipe at the end of our print statement so after we print this here we're just not going to print anything and now we need one more check to see if we should go to the next line or not because every row we want to go down to the next line so i'm actually going to say here print and we can just do an empty print statement now by doing an empty print statement it just brings us down to the next line because it prints a new line character by default at the end of the empty print statement seems a bit weird that's kind of how this works we're going to print the first row this will go on the same line then we're going to print a new line character so it brings us down to the next line print the next row new line character next row new line character next row etc right so let's have a look at this now okay let's go here let's run how much you like to deposit a hundred dollars uh let's bet on two lines how much would you like to bet ten dollars and there we go now we have our slot machine excuse me and this is correct right so we have all of our columns uh and then all of our rows and what we'd now be checking is if we have three in a row right that's what we're going to have to check okay so we have our slot machine we've spun it we probably should have some input that asks like do you want to spin the slot machine and then they can hit spin blah blah be a bit more interactive but that's how it works and just be clear here we're going to check if we have three in a row and if we do based on the value of the symbol we're going to multiply their bet and then give them that amount all right so let's write a function that can do this now when we're checking if they won or not we need to know what their bet is which lines they bet on right and then we can actually check now the way the lines works again i know this is not exactly how like a real casino slot machine will work we're going to make it so that if you bet on one line you just bet on the top line if you bet on two lines you put on the top in the middle if you bet on three lines you bet on all three i'm not going to let the user pick like where the one line is they want to bet on we'll just say one is top two is both the top two three is all of them and that's how it'll work okay so let's do this let's go define check winnings okay now to do this we need the slot machine itself we need the lines so we're gonna say lines and we need the bet okay so how are we gonna do this well we just need to look at the rows that the user bet on so let's start with that and then we can check each row and determine a value also for the symbol so we're actually going to take one more thing we're going to say values now i'm also going to make the values here so i'm going to say symbol underscore value and i don't know what actually makes sense again to do for this but we're going to say that d is going to be a two times multiplier c is gonna be a three times multiplier this will be four and then this will be five so you know the more rare the symbol is the higher your bet gets multiplied again i don't think this is a fair slot machine but that's fine okay so check winnings now we have columns lines bet and value now let's look through only the rows that they bet on so the lines that they bet on so to do this we're going to say 4 and we'll say line in range then this is going to be lines like this now the reason this will work is because if they bet on one line then we're going to go up to one line but not include it so that means the line will be equal to zero right so if i bet one line then this is one and that means this is only ever going to be zero if they bet on two lines then line will go to zero and one so we'll check the zeroth row which is line one and the first row which is line two if they bet on all three lines then line will be zero then one then two so we'll check all three of those rows right and this will work dynamically even if there was more lines to bet on so inside this for loop we need to check that every single symbol in the line or row that we're checking is the same so we can get the first symbol that's in this row and then just make sure it's the same for the rest of the symbols so to do that we're going to say symbol is equal to columns 0 at the current line now the reason we're using columns 0 is because we have all of the columns not all of the rows it makes it a bit more complicated so we need to look at the first column because that's where the first symbol is always going to be for each row and then get whatever line we're on so first column and then either you know line 0 line one line two etc going down that first column gives us the first symbol and we're going to assume that we always have at least you know one reel right so at least one column so now that we have the symbol we're gonna loop through all of our columns so we're gonna say four column in columns like that now what we want to check so the symbol to underscore check is equal to the column at whatever row we're looking at so if we're looking at row 0 we're checking the first symbol in the column if we're looking at row one checking the second symbol in the column etc symbol to check we're then going to say if the symbol is equal to the symbol to check and actually we'll say if it is not equal to the symbol to check then we are going to break okay and the reason we're breaking is because if we found one of the symbols is not equal to the previous symbol uh or equal to all of the symbols that should be in this row then we just break out of the for loop and what we'll do is we'll put an else statement here and what this else statement will do is it will tell us if we didn't break out of the for loop so i'm sure you probably haven't seen this before but you can do a four else where if you break this else statement does not run but if no break occurs within the for loop then this else statement executes so in the else statement here what we'll do is we'll figure out how much they won so we'll say winnings is equal to zero okay and then here we're going to say winnings plus equals and then this is going to be the values at the symbol multiplied by the bet and then we can go here and return the winnings okay so let's look at this because i'm sure this is confusing we have every line in the lines which means we're looping through every row essentially that we're going to be checking that the user bet on we then say the symbol that we want to check is whatever symbol is in the first column of the current row because all of the symbols need to be the same right we then say 4 column in column so we now know the symbol that we're going to check now we need to loop through every single column and check for that symbol so we go to each column and we say the symbol to check is equal to the column at the current row that we are looking at so if it's row zero we're doing row zero if it's row one row one et cetera we then check if these symbols are not the same if they are not the same we break out which means that we're going to go check the next line because they didn't win because symbols were not the same if they are the same then we don't break and if we get to the end of this for loop and we've not broke out which means all of the symbols were the same then that means that the user won and what they won is whatever the multiplier is for that symbol times their bet right and this bet is the bet on each line not the total bet right so they could win on one line but lose on the other line that's how it works okay hopefully that's clear i can't explain it much more than that just understand symbol values here right uh gives us you know five four three two so that's what i'm using i'm saying values at whatever the symbol is multiplied by the bet that's how we're adding to the wings okay so that's it for determining how much they won so let's run the game one full time now and then we'll figure out how we can run it multiple times and reduce the balance and all that which is easier than what we just did okay so we print the slot machine and now we're going to say winnings is equal to and what did i call this check winnings okay check winnings we need to pass this our slots the number of lines so i think we called that lines right the bet which we have right here and the values which is called symbol values so let's go symbol underscore value all right then we're going to print u 1 and then whatever the amount is that they want so let's make this an f string okay and then you won and then this is going to be winnings like this and this could potentially be zero dollars right okay so i think that's good i'm thinking that we might want to also tell them how many lines they want on or what lines they want on so let's actually return that as well from this function so get uh check winnings we're going to do one more thing here we're going to say winning underscore lines is equal to an empty list and if they win we're going to say winning lines plus equals uh actually not plus equals dot append and then this is going to be whatever the current line number is but we're gonna have to add one to it because this is an index and we want like line one two three not zero one two so we say winning lines dot append line plus one then we go here and we put winning lines and now we're returning two values the total amount they won as well as what lines they want on okay now let's go here to winning lines and now we're going to say winnings and winning underscore lines you won these winnings and then we'll say u1 we'll print another line like this f u 1 on and then this is going to seem a little bit weird but i'm going to do asterisks and then i'm going to pass my winning lines now again i promised i was going to show you guys some new stuff so i'm showing you some advanced syntax here but what this does is actually called the splat operator or the unpack operator and it's going to pass every single line from this winning lines list to this print function so what that means is if we have lines like one and two it's going to pass both one and two so it's going to say u1 on and then one two so i'm going to say you want on lines like this and then it would say one space two we want on all three lines say one space two space three if we didn't want any lines then it's not going to say anything okay you'll see when when we print this out how this works so let's run this here let's make this full screen now and let's go through an example so deposit 100 how many lines four invalid okay let's bet on two lines how much would you like to bet on each line ten dollars all right so we won zero dollars because none of the symbols are the same so let's try again all right pause it hundred dollars uh two uh two lines sorry how much would you like to bet ten dollars you won zero dollars okay so this is going to happen a few times so rather than continuing to do this and hoping that we're going to win let's make it so we can run this multiple times without having to constantly rerun the program so we have this main function we have the balance and we have the lines now the balance is going to stay the same but all of this stuff here needs to happen multiple times right and we'll reduce from the balance and all that stuff so let's copy all of this into a function and let's say define let's say game okay we'll put this inside of here so this kind of executes one game now what we can do is write a while loop here and this while loop can kind of handle running the game all right so i'm just thinking here if we do this we need some way to determine like from this instance of the games like per spin so actually let's just say this is maybe spin we need to know how much they won or they lost so i'm just going to return here the winnings minus the total bet and that will tell us how much they won or lost from this spin because if they want 100 but they bet 15 then they only won 85 so then here we would update and we would add 85 right but if they didn't win anything then it's going to be negative whatever the total bet is so like 15 bucks so we'd subtract 15 from the balance okay so here i'm going to say well true and we'll print um current balance is and then we can just put comma actually let's do this as an f string so we can put a dollar sign current balance is and then we'll do dollar sign and then balance okay and then we'll play games we'll say spin equals input say press enter to spin okay so they'll press enter to spin so actually we don't even need to put a value here because we don't care what they type in or actually we can say this spin is equal to press enter to spin and we'll say q to quit so if they type in q then we'll quit so we can say if spin is equal to q then we will break the while loop which will just end the game okay otherwise we'll spin so we can say spin like that and we'll say balance plus equals spin so the way this works now is that spin is going to return to us however much they won or lost it will tell them like what they want or lost so then we'll just update the balance based on the result of that spin and then we'll run this again and say okay current balance is this press enter to spin blah blah and then when they spin they're going to have to enter this info and actually press enter to spin probably doesn't make sense to go here right because you only want to spin after you enter your information although we can just have this works we'll say press enter to to play that's fine and then they can do it inside of there uh okay now that we have that i think that's actually all that we need i'll just make one last print statement here that says u left with and then we'll put inside dollars and then this will be the balance and we'll put an f string here all right let's run this and give it a shot okay so run go here how much do you like to deposit let's suppose it a thousand dollars okay current balance is a thousand dollars press enter to play q to quit okay enter uh and we got an object here balance plus equals spin all right looks like we need to fix this so balance is equal to deposit balance plus equals spin we have winning lines subtracted by total bet and what is the problem all right so the issue here is that i accidentally have the name my function spin the same name as this right here so we're just going to change this to be answer so now this will work properly okay so my apologies guys let's go here and fix this and let's say thousand dollars current balance is a thousand press enter play okay enter the number of lines to bet on let's bet on two lines let's bet 10 bucks okay balance is not defined looks like we need to fix another error so what we need to pass here is balance to the spin function so that it can actually check this when we make a bet so of course that makes sense just forgot to do that so let's pass balance here and now we'll actually be able to check that all right so let's try it again third time's a charm hundred dollars current balance hundred dollars press enter to play okay number of lines let's go three let's go ten dollars and you are betting on ten dollars you're betting ten dollars on three lines total is equal to thirty dollars okay you want zero dollars you won onlines and it has an issue here on sport operands for list and int okay my apologies that's because i put winning lines this needs to be winnings all right fourth time's a charm so let's make sure our variable names are correct that was just a silly mistake let's clear and run hundred dollars enter three lines ten dollars okay you won zero dollars you won on lines your current balance is 70 okay let's play again three lines five dollars okay did we win anything no we did not all right let's play again i'd really like to try to win something but as we can see the slot machine is not in our favor today ten dollars you won zero dollars okay current balance is 25 all right three lines two dollars you won four dollars okay nice so how did we end up winning uh four dollars we bet two dollars and we got a line here of ds so that gave us uh times two multipliers so two times two four so we won four bucks there you go and then our balance get up gets updated sorry to 23 dollars because we were at 25 uh subtracted six add four yes okay so that makes sense all right so let's play again let's go three lines one dollar okay you won nothing let's play again three lines one dollar you want nothing three lines one dollar uh oops uh this is let's go three dollars you won nothing okay play again one line three dollars you won nothing all right well i won't continue to play but you get the point we've just created a slot machine and now if i hit q and enter we leave with 11 dollars all right so i think i'm gonna wrap it up there i'm gonna zoom out a bit so you guys can read more of this code i'll just kind of scroll through it i'll also put it in the description in case you want to reference it on github but you can see this is what we wrote so we did about 150 lines of code in i guess about an hour now i walked you through my thought process i showed you a bunch of different python features we did actually some fairly advanced logic in terms of looking through rows and columns and looking at nested lists i showed you about functions i showed you about the anonymous variable you saw this splat operator here and when we went on a line you saw it said we won on whatever the line number was so hopefully this was helpful to you guys hopefully this showed you kind of how to structure a program notice that we put everything in separate functions we've kind of spread our code out we've made it very readable and now we know if there's a bug anywhere it's really easy for us to go and kind of figure out where that is because we've separated everything into different units right different blocks of code anyways i will wrap it up here i hope you guys enjoyed this video if you like this style of teaching then make sure you check out programmingexpert.io and i look forward to seeing you in another youtube video [Music]
Info
Channel: Tech With Tim
Views: 395,916
Rating: undefined out of 5
Keywords: tech with tim, python, learn python, learn python with 1 project, python how to, how to do python project, pulumi, pulumi promo code, octoml, free coupon code, octoml coupon code, python porject, python project tech with tim, tech with tim python, python project for beginners, beginner python project, learn python through projects, python tutorial 2022, python tutorial project
Id: th4OBktqK1I
Channel Id: undefined
Length: 55min 4sec (3304 seconds)
Published: Sat Sep 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.