3 Mini Python Projects - For Intermediates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this video i'll be sharing with you three mini python projects for intermediate programmers these projects are designed for people that have some experience with python they understand the basics like if statements for loops functions etc and they want to build something a bit more complicated that doesn't take a ton of time each of these projects will take between 20 and 30 minutes for us to build out there'll be under 100 lines of code yet they'll still be more advanced than kind of a beginner project and a lot of the other stuff that i have on this channel so with that said let me give you a quick demo of the three projects and explain what you'll learn in them and then we'll get into actually building them out so the first project that we're going to work on is a pathfinder or a maze navigator i'll just run the code and kind of explain how this works so you can see it actually takes over the entire terminal here we have a maze and you can view what's known as the breadth first search algorithm going through here and trying to determine the shortest path between one point and another now you can customize this however you like but i'm going to show you how we can control the terminal like this have different colors and then override what's currently on the screen as opposed to constantly printing stuff out this is really cool it's also really simple to do and then of course i'll walk you through the pathfinding algorithm which is kind of a very famous algorithm that you learn in any computer science class again it's called breadth first search and i'll explain to you how that works so that is the pathfinder or the maze algorithm whatever you want to call it the next thing that i'm going to show you is a little bit simpler but this is how you can get data about nba games so basketball games so the current games that are running what time they start at as well as viewing stats and all kinds of other information about players teams etc i've built this out just to show you how to work with an api and then of course you can customize this and change it how you like but if i run the program right now you can see that what it gives me is a list of the teams ranked by their points per game so we have all of the different teams and they're ranked currently by number of points that they average per game that's one functionality that we can use the other one is to get the current scoreboard of the teams so let me just change the function here and let's run this and what this prints out now is all of the games that are upcoming what time they start at and then if the game was currently running it would show you what period it's in the time on the clock and then whatever the score is in the games now of course none of these are running right now when i'm running this uh but if they were going on then you would see the information and every time you rerun this it'd be updated in live time so kind of a cool project especially if you're into basketball and then the last one here is a currency converter so let me run this project here you can see we get welcome to the currency converter and then we have three commands list convert and rate so i can enter a command let's start with list this lists out all of the currencies then what i could do is maybe convert so let's say i want to convert the currency of usd maybe a hundred dollars in usd to zimbabwean dollar so let's try that said wl okay it gives me 100 usd is 32 199 zwl i could also get the rate between different currencies so enter my currency cad and usd and then notice this is the current exchange rate so that's what you can do with the currency converter all of these projects you can add to and make a lot more advanced this is a really good starting kind of foundation and teaches you how to work with apis and do some more advanced things that you wouldn't learn in a beginner tutorial so hopefully after seeing the demo for these projects you guys are excited for this video if you are make sure you leave a like i will mention all the source code for these projects will be available in the description and we'll get into them after a quick word from our sponsor before we dive in i need to thank ltm designer for sponsoring this video ltm designer is the world's most trusted pcb design system that enables engineers to effortlessly connect with every facet of the electronics design process ltm designer has used 35 years of innovation and development to create a truly unifying design environment that makes it the most used pcb design solution on the market ltm designer provides an intuitive and powerful interface that lets you design pcbs rapidly while interacting and collaborating with your mechanical designers the interface provides a photo realistic 3d environment collaboration and synchronization with tools including solidworks ptc creo and autodesk inventor realistic rigid flex designs multi-board assembly and much more ltm designer is the most popular ecad tool and electronics design software and you can get started with it today for free by pressing the link in the description and registering for a free trial thanks again to ltm designer for sponsoring this video and now let's dive in all right so let's go ahead and dive right in now i'm going to start with the pathfinder or the maze navigator whatever you want to call this and we're going to use a package here called curses which allows us to control the terminal and colored in the way that you saw and actually override what's currently in the terminal so we don't have to keep printing a bunch of separate lines and kind of having a really messy standard output now if you're on mac or linux cursor should be installed by default for you but if you are on windows you need to install this so go to your terminal i'm just in vs code and i've opened up a terminal here you're going to type the following command pip install and then windows hyphen curses like this now again you only need this if you're on windows so hit enter try that command that should install it for you and if for some reason that doesn't work i'm going to give you two or three alternative commands you can try pip 3 install windows cursors if that doesn't work you can try python hyphen m pip install windows cursors and if that doesn't work you can try python 3. now if none of those work i'll leave two videos on the screen that should show you how to fix this pip command one for mac and one for windows although i guess you only really need it for windows because you don't need to install anything if you're on mac or linux all right now what i've done is made a python file here called pathfinder.pi i've opened it in vs code you can work in any editor that you want but i'm going to use vs code i've also pasted in a maze you can find this maze from the link in the description on the github repository or you can just make your own maze i'm treating all of the pound sign here's our number sign here as my obstacles in the maze all of the empty strings are going to be things we can navigate and then the o is going to be our start and the x is going to be the end now i do want to mention here that if you are trying to get better at python you also can check out a product i have called programming expert dot io this a programming course teaches fundamental programming object oriented programming advanced programming software engineering tools all kinds of stuff just wanted to mention that if you guys are interested you can use discount code tim i'll leave that in the description for now though we do want to build out this project so we have our maze here and the first thing i'm going to do is i'm going to import that curses module and quickly walk you through how this works so you can see how we can print colored output i do also have an entire tutorial series on the curses module on my channel in case you are interested i'm also going to say from curses like that import and i'm going to import something called wrapper and then while we're up here we're going to import a queue this is built into python you don't need to install it and we're going to import time just so we can implement a bit of delay here in this program when we start actually trying to visualize things for now though i want to show you how the curses module works so what we need to do here is define a main function and this main function needs to have as one argument std scr now this stands for standard output screen and this is what we're going to use to actually output all this stuff for our program so rather than using the standard print statements here we're going to add stuff to this screen this screen is going to override and kind of take over our terminal and then as soon as the program ends everything on that screen will be cleared and will be brought back to the original terminal you'll see what i mean in a second but for now we're going to type the following std screen dot clear so we're going to clear the entire screen then we are going to not write we're going to add string and when we add a string to the screen here we have to pass the position that we want to enter this string at or that we want to add it to so for now i'm going to go zero zero that represents the top left-hand corner of the screen so i believe we're going row and then column that we want to enter this in i'll experiment in a second then whatever the text is that we want to show on the screen so in this case i can do a hello world exclamation point then we're going to do std screen dot and then this is going to be refresh and what that does is refresh the screen and so we actually see what we wrote and then lastly i'm going to say std screen dot and this is going to be get ch what this stands for is get character and this is similar to an input statement that we would have in python it just makes it so that we're going to wait here until the user hits something before we exit the program if we don't have that it would do this and then just instantly exit and we wouldn't see anything because we weren't waiting long enough to actually view the output all right now that we have that main function we're going to call wrapper which we imported up here and we're going to pass to it the name of this main function but we're going to make sure we don't call the function so i'm not calling like that i'm just passing the name of the function and what this does is it initializes the curses module for us and then it calls the function passes this std screen object and then we can use that in here to control our output all right hopefully that's clear so far let me save and let's run the code and let's see what we get and notice that it kind of brings me to this blank window and it says hello world and then if i hit any key on my keyboard brings me back to the original terminal one more time if i run this notice it shows me hello world hit a key and then i'm out that is the curses module very basic introduction however i want to show you what happens when we change these coordinates and how we can add colors so let's say now we maybe draw this at five so five zero is our position let's run the code and notice we go five rows down and we're at the zeroth column so that's how you can control where you're drawing this you put the row and the column you want to start drawing so if i go maybe 5 5 and then let's save and rerun oops save and rerun notice we get hello world kind of more in the center of the screen so very powerful what you can do here and then if we want to add a color this is a little bit more complicated we first need to initialize a color so i just got to look at my cheat sheet over here to remember how to do this so we're going to say curses dot init underscore pair and whenever we create a color pairing here in curses we give it an id so i'm just going to give an id of one and then i put the foreground and the background color that i'd like to have so i can say curses dot and then color underscore and then whatever colors you can see there's a bunch of options here green magenta red whatever so let's go with blue for now and then the foreground color or sorry the background color my bad it's going to be curses underscore color and then black so you put your foreground color and your background color pretty much all the colors you can think of are you know you can access them with color underscore their name and then now that we have the initialization pair if i actually want to use this color what i have to do is the following i'm going to say blue underscore and underscore black is equal to this is going to be curses dot and i believe this is color pair again let me just have a quick look at my cheat sheet here to remember this yes it's colored pair and we're going to put the id of the color pair we want to use so since i reference this with id1 or create it with id1 now i reference id1 and that's going to give me a color of blue and black and the way i apply that now to my text is i just pass another argument to add string with this so blue and black and now when i start displaying this it should be blue and black so let's do this let's run the code and notice i get hello world in blue text and the black is just the default background so it doesn't change the background color but if i wanted to be like a white background and i would say color white and then if i draw this now so let's rerun this notice i get kind of a white background for that text great but let's go back to color block okay so that's the first color pair while we're here let's initialize another color pair because i want blue and black i also want red and black so let's change this to red and let's make that two and then we'll use these later on i don't actually want to write hello world onto the screen so let's actually just comment all of that out for now and let's work on the breadth first search algorithm and then we can actually start displaying stuff on the screen so the first thing i want to handle is printing this maze out so obviously i want to view the maze on the screen so to do that well how am i going to do that i'm going to write a function here i'm just going to call this print maze and to be able to print the maze i need the maze that i'm going to print i need the standard screen or the standard output screen that i'm going to print the maze to and i'm going to take a variable here or an argument called path now path is going to be the path i want to draw on the maze obviously we don't know what the shortest path is yet but i can pass a path here of coordinates and then whatever values are in that path i'll draw them a different color in the maze so you'll see what i mean in a minute so that's print maze uh now inside of here i'm going to grab my two colors that i want to use so i'm just going to say blue is equal to and then this will be curses dot color underscore pair this will be one let's say red is equal to curses.color pair two just so i can use both the blue and the red we'll draw the default maze as blue and then the path as red okay continuing i'm gonna loop through everything in my maze because this is a two-dimensional array so i have kind of all my rows and then the columns would be the individual values in each row so i'm going to say 4 and this is going to be i comma rho in enumerate maze now if you're unfamiliar with enumerate this is going to give me the index as well as the value so i'm going to get every one of these rows as a row or every one of these nested arrays as a row and then i will be what row i'm currently on okay continuing i'm going to say 4j comma value in enumerate and then i'm going to enumerate over the row the reason i can do this is because i have a list or an array whatever you want to call it but list in python so row is my list then i'm going to enumerate over the list that i have i'm going to grab whatever column i'm currently on which is what j is going to represent and then the value is going to be whatever symbol here is in the column okay so that's what i have then what i want to do is i want to simply draw this onto the screen so i'm going to do the following i'm going to say std screen dot and we're going to add string and now we need to determine where we want to add this string well where i want to add it onto the screen is going to be whatever position i'm currently on which is going to be represented by i and j so i is going to tell me the row i'm on j is going to tell me the column so what i can do is simply add whatever i'm drawing here at whatever row and column i'm currently at so i'm going to say i j as the position and then i'm going to say value and value is just going to be either an empty string a numbers sign an o and x whatever it is that i'm currently looking at hopefully that makes sense but that's what we want for print maze and then i'll handle the path in one second for now though let's actually try printing the maze so i'm going to uncomment the clear i'm going to uncomment the refresh and the get ch and now rather than adding my own string i'm going to call printmaze i'm going to pass to it the maze variable which is just a public variable or a global variable that's fine i'm going to pass my std screen and then this is a default argument of empty list so i don't need to pass that okay so let's run this now let me just make the terminal a bit bigger first let's run this and notice my maze is printing out now this looks a little bit squished together like it's too close to me so to spread this out a little bit what i'm going to do is simply multiply the row in the column that i want to draw this at by 2 and actually we can leave the row as just regular eye we can just spread the columns out a little bit more so now we should get kind of two spaces between each of our elements and also let's add a color here so let's add blue and now it should be drawn in blue so let's run this and i guess it needs to run again okay and there we go that looks a lot better the maze is spaced out and we can kind of see it all nice great so let me exit that all right so now that we are printing the maze out and i guess that's pretty much all we're doing i need to talk a bit about how this algorithm works so let me head over to the whiteboard i will explain that to you and then we'll start coding it out so in front of me i have a very simple maze i'm also going to refer to this as a graph and this is what i'll use to demonstrate the breadth first search algorithm so the goal here is to find the shortest path from some starting node to some ending node and i'm going to reference these nodes by their position in the graph so we see these are different rows these are different columns and i'm going to have my positions in row column format which means i would refer to this here as one zero so row one column zero just keep that in mind as i write the positions because uh you might be confused if you're reading it the other way and like here this would be zero three all right so how does the breadth first search algorithm work well the concept behind the breadth first search algorithm is that we're going to continually expand outwards from a point until we find the ending points that we're looking for or the ending node i'm just referring to every single one of these squares as a node okay in kind of graph theory that's what you would call them so we're going to start here at this start point and we're essentially going to slowly expand outward by looking at all of the neighbors of this point so we're going to look at this neighbor and this neighbor and if there was a downwards neighbor or a left neighbor we'd look at those as well but there isn't any left or down neighbors so obviously we're starting here so we expand here and then from both of these points we're going to look at all of their neighbors so maybe we start with this one we would look at this neighbor then we would look at this one then we continue the process and we do this one iteration at a time which means that once i finish considering this neighbor here i go and i do this one then of course we're going to move up to here we're going to move up to here and since we're doing this one step at a time as soon as we hit the ending node we will actually be guaranteed to have found the shortest path because we're only expanding our path from the starting node by one new node at every single iteration now that's a super high level recap of how this works of course i'm going to walk you through the code in a more complicated demo but that's kind of the concept here one step at a time we're slowly expanding outwards from this node and expanding our current path by one until eventually we reach this end node and as soon as we find the end node we know that we have found the shortest path because every other path that we have is either going to be the same length or longer to find this path okay so let's now look at a demo of how we actually do this so first of all we're going to use a data structure called a q now q i believe is spelt like that i'm also just going to refer to it with the letter q and this is what's known as a first in first out data structure or a last in out data structure whatever you prefer to call it essentially what that means is that if i enter some item into the queue just kind of like a phone cue or any other lineup that you would be in if i enter this item first then this is going to be the first item to come out and the second item that i enter into the queue that's going to be the second item to come out now that's important because we're going to process elements in the order in which they're entered into the queue so just keep that in mind so for our cue we can have a back and a front so we can say that this is the front of the cue excuse my messy handwriting and then this is the back just so we're clear on kind of what element we're going to process next so what we're going to start by doing is we're going to place our starting node here inside of the queue now the way i do that is i just place the position of the node that i want to start looking from so i'm going to put a position of 3 and then 0 because it's in row 3 and column 0. now every single step in my algorithm what i'm going to do is i'm going to pop an element off of my queue i'm going to take the element that's currently at the front of the queue i'm going to remove it from the queue and i'm going to start processing it now the way i process an element is i essentially just look at all of its neighbors and try to find if we're currently on the end element or not so in this case i take three zero off the queue and i say okay i want to process all of this element's neighbors and kind of expand outwards from this until eventually i get to this node so what i'm going to do is look at the neighbors i'm going to determine i have two of them and i'm going to check if these neighbors are the end node if they are then i'm done the algorithm i found the shortest path but if they're not what i need to do is add them onto the queue so now they can be processed and we can continue expanding until eventually we find this endnote so let's erase this from the queue here so 3 0 we've now processed that element i'm going to add this into some set here so let me just create a set this set will be known as our visited set and okay it's kind of hard to draw this the way that i want to so let's just do this 3 0 and the reason i'm putting it in a set is just so i know okay i've processed this element i don't have to look at it another time anyways processing the element again involves looking at all the neighbors so the two neighbors here are 2 0 and what do we have we have 3 1. so i'm going to add those positions into the queue doesn't matter the order that i add them in so i'm going to add 2 and what was this 0 and then my other one is 3 and 1. okay so these are now the two new elements that i need to process to continue expanding my path so what do i do well i'm going to pop now the first element off of the queue and i'm going to process this one and well repeat everything that i was doing so let's cross it off we'll erase it in a second this is the current one that we're looking at so now we need to consider all of this node's neighbors now it has a neighbor down and a neighbor up it also has one to the right but since the one to the right is an obstacle we don't look at that and for the one that's below us here we see that the one below us is an element that we've already looked at and the reason we know that is because it's in this visited set so i can skip the neighbor below us because we've already looked at it i don't need to process the same element twice and i can just go directly to the element above so i'm going to add a single neighbor here to my queue and this neighbor is going to be in 1 and then 0. so now i've processed this element it was not the final node that we were looking for so let me delete it here and now we're going to add this into this visited set now i didn't mean to butcher my element here so let's go here what was that position that was two and zero so let's add this in to zero like that and now we've visited those two notes now we're going to continue so we're going to take the next element off of the queue which is 3 1 so i'll just delete it here we will add it into this set so you might as well just add it right now okay 3 and 1 and then from 3 1 that's right here we need to look at all of its neighbors so it has one going to the right and it has one going to the left well of course the one to the left we've already processed so we can just add the one to the right which is right here and that has a position of 3 2. so what i do is i add 3 2 to my q if we can that's a really bad three we get the point we add three two and we continue and this process is going to repeat until eventually we hit this endnote and again as soon as we hit this end node we know that we found the shortest path because that's the first time we found it and we would have found it on another path if there was a shorter path to get to it so i mean i could continue this example but hopefully you kind of get the idea we're expanding outwards one node at a time until eventually we find the ending node and then i will show you in the code how we actually kind of keep track of what path we were on because that is a little bit more complicated but this is the basic idea we have a queue we add elements onto the queue we process them one at a time in the order in which they were inserted into the queue this guarantees that every kind of new element that we're looking at is only expanding our current path by at most one node as soon as we find the end node then we can just say okay well we found the shortest path let's end the algorithm let's draw the path out hopefully that gave you a very brief explanation of how this works i don't want to go through the whole thing because it will take too much time now though let's start coding this out and hopefully everything will be cleared up all right so let's code out a function here called find underscore path and we're going to take in here the maze and then this is going to be the standard screen and i think that's all we need for find path now for find path i'm just going to declare a few variables here i'm going to say start is equal to x end is equal to o and actually is this no we're going to say start is equal to o and is equal to x and what else do we want here we want to figure out what the starting position is of our maze now we could just hard code in the position right here but instead i'm going to write a function that will essentially look for this o and tell me where in my maze i'm starting from because we're going to start from this position we need to know the coordinates of this right so in this case it'd be 0 1 2 3 4 5. okay it's in row 0. so it would be 0 5 that would be the position here of the start but we want a function that will tell us that so we don't have to manually do that so for now i'm going to say define find underscore start and just because i didn't really explain this i just jumped right in what we're doing here is we're going to start coding out that breadth first search algorithm that you just saw so that's what find path is going to do but first we need to find the starting position so we're going to take in the maze here and we're going to take in whatever the starting symbol is and what i'm going to do in here is simply say 4 and this is going to be i comma rho and enumerate maze and then for j comma value and enumerate row i'm going to say if the value is equal to start then i can simply return i comma j so i'm going to return the row and the column where i found this starting symbol and we're going to assume that we just have one of those symbols okay now otherwise we'll just return none if for some reason we couldn't find the start okay so now in here we're going to say start underscore pause is equal to find start then we're going to call the maze and the starts now we know the starting position now what we want to do is set up our queue and set up those iterations and finding the neighbors and all that stuff i was discussing so for the queue we have this q module and we're going to use it so the way we do that is we're going to say q is equal to and then q dot q and again this is a first in first out data structure so the first element i insert is the first element that comes out that's very important because the order in which we process the nodes really matters when we're doing the breadth first search we don't want to process the most recent node we've added to the queue we want to process the node that's been in the queue the longest just like we would if you were accepting you know a bunch of phone calls or if there was a lineup or something like that that's exactly what a queue is right so we have our queue and we need to insert something into the queue so to insert something we use this put method here and we're going to put a actually a tuple and the tuple is going to contain the starting underscore position and it's going to contain a list that has the start underscore pause in it now the reason why we're adding two elements in here is because in the queue i want to keep track of the current position or the node that i want to process next as well as the path to get to that node now the reason i want the path to that node is because as soon as i find the ending node i'm going to take whatever path i currently have and i'm going to start drawing that onto the screen i'm going to show that on the screen so for now since we haven't found the end node our current path just includes the start position but every time i process a new neighbor or something along those lines i'm going to change the path and the path will continue to grow and i'll be able to see what path we're at for every single note hopefully that makes a bit of sense but that's why i'm starting two pieces of information the position i'm currently on as well as the path and in fact we could even optimize this further and just store the path and grab the last element from it but i'm just going to do two things here just so it's crystal clear when i code this out it's not the most efficient thing in the world but for this tutorial it's fine okay so for now we put that in the queue we also want to make a visited set which is going to contain all of the positions that we've currently visited now that we have all that set up i'm going to say wow we still have any elements in the queue so while not and then q dot and i think this is either is empty or just empty i believe it's just empty so while the queue is not empty what we're going to do is we're going to get the most recent element from the queue the one that's kind of next in queue so i'm going to say current underscore position and path is equal to q dot get now gets just going to give me the element at the front of the queue and since i have two things for every element i have my path as well as the position i'll grab the current position which will just give me the first element and the path which will give me the second okay now that i have the current position i'm going to say the row and column is equal to current position so i'm going to break down the position into its two components the row and the column and i'm going to find all of the neighbors of this position and start processing them if they are not the end node so first i'm going to say if maze and then this is going to be at row call is equal to and this is going to be the end so we're going to say end like that so we're going to look in the maze and see if this position is equal to an x if it is that means we found the end and so what we can do here is simply return the current path because again as soon as we find the end node we know that we've found the shortest path or one of the shortest paths because there could be two paths that are both the shortest we'll find one of them so we can just return the path and then we can draw that if we want okay continuing now what we need to do if we haven't found the end node is we need to continue branching out so we need to find all the neighbors of the current node and then expand towards them so we're going to write another function here called find underscore neighbors now the tricky thing with the neighbors is that we need to make sure that the neighbor that we're finding is not an obstacle and that it's a valid position in the maze so you'll see what i mean here we're going to take an amaze a row and a call and we're going to say our neighbors is equal to an empty list and we're going to determine programmatically what all the neighbors are of this current position so we're going to look up left right and down if you wanted to you could do diagonal but i'm not doing diagonal in this tutorial but adding that is is fairly easy so for now i'm going to say if the row is less than not zero sorry if the row is greater than zero then i'm going to add a neighbor and this neighbor will be the neighbor above i'm going to say neighbors dot append and i'm going to append the row minus one and then the current column so we can add a comment here and say this is gonna go up now the reason i have to check if the row is greater than zero is because if the row currently is zero i have no more rows to look up i'm at the bounds of my maze and so i can't add that but if the row is greater than 0 so it's 1 at minimum then i can subtract 1 from it and that's still a valid position next i'm going to say if the row and this will be plus 1 is less than the len of the maze then i can add the row below so i'm going to say neighbors dot append row plus 1 and then call again because if the row plus 1 is equal to the length of the maze then that means if i were to add row plus one i would get an index out of bounds error i'd be too far that means i'm already at the bottom of my maze but if this is true then that means i can add one more without going out of bounds so that's for the rows now we want to do the same thing with the columns for so for left and right so i'm going to say if the column is greater than zero then this is going to be left what i can do is say neighbors dot append and i can append the current row and the column minus one okay and i'm going to say if the column is column plus 1 is less than the len of maze 0. now the reason i need to check may 0 is because my maze may not necessarily be square in this case i don't actually think it is square we may have more columns than we have rows so i need to check how many columns we have and i can just do that by grabbing the first row and seeing how many elements are in there so that's what i'm doing here if we don't have a first row this will cause an error but we're always going to have first row so that's fine and then i'm going to say neighbors dot append row and call plus one and then here i'm going to return neighbors and let's just put a comment here for right now in here we did not check if these positions are obstacles we'll do that inside of here for now we've just got all of the valid positions that are neighbors now we're going to check if they're obstacles or not as we loop through them so i'm going to say neighbors is equal to and then this is going to be find neighbors enough find path find neighbors and we are going to pass to it the maze as well as the row and the column of the current position that we're processing right finding all of their neighbors we're going to say for neighbor in neighbors and we are going to check if this is an obstacle or not and if we've already visited it before because if we've already visited it we don't need to process it so i'm going to say if the neighbor is in visited then continue because we don't want to process it and i'm going to say if the neighbor and actually sorry i need to break this down i'm going to say row call is equal to neighbor and actually we'll just go rc so that we don't shadow the variable name up here but i'm going to say rc is equal neighbor standing for row and column i'm going to say if the maze at and this will be rc is equal to a pound sign okay then what we want to do is continue okay now if this is not the case so if the neighbor's not in visited and the neighbor is not a pound sign then that means we need to process this neighbor so i need to add it to my queue so i'm going to say q.put and i'm going to put inside of here my position so i'm going to put the neighbor okay which is going to be a tuple containing the row and column and i also need to put the new path that now contains this neighbor so i'm going to say my new path is equal to and this is going to be whatever the current path is up here okay so where do i have path path is right here and then this is going to be plus and inside of another list because i can add two lists together i'm going to put the what is it the neighbor so all this is doing is kind of tacking the neighbor onto the current path this is not the most efficient possible way to do this you guys can try to optimize this if you want but for now this is fine and for the simplicity of the video i'm going to do it in this way and then i'm going to put new path so i'm adding the neighbor as well as the new path and the new path is just whatever the current path is plus the current neighbor that i'm considering right that's what i have when i'm adding to the queue okay hopefully that makes sense lastly what we need to do is say visited.add and we add the neighbor to that to make sure we don't process it multiple times and that's actually all we need for the breadth first search algorithm again i know this looks pretty complicated but that's all we need now what we want to do is make it so that we draw all of this stuff out so we can kind of see this happening and then i'll walk through the algorithm one more time just to make sure it's crystal clear so where we had this here right where we were printing the maze i'm going to copy that and i'm going to put that inside of this wall because at every step i want to draw out what i'm currently considering so i'm going to go up here and i'm going to paste this before we're checking if we're at the final element or the the final node and i'm going to print the maze except this time i'm going to pass my path and when i pass the path now this will allow me to draw out the path right so i'm going to clear the screen i'm going to print the maze and i'm also going to add something to printmaze so we draw the path and then i'm going to refresh the screen and i'm going to do this every while loop iteration so that i can see the progress going on as i go through this algorithm so now let's go to print maze and let's make it so that we print the path in a different color so here i'm going to say if and this is going to be i comma j is in the path so if it's in the path that we have right here that's valid to check that then what i want to do is draw this in a different color otherwise i'll do this so i'm just going to copy this line i'm going to paste this in here i'm going to change from value to be x because x is going to represent the current path and i'm going to change the color to be red so now rather than drawing an empty string i'm going to draw an x and i'm going to draw it in red and i'll draw it at the same position i would have before but again we're just changing what we're drawing okay so that's pretty much all we need last thing we need to do here is just call this function so this function is find path and it takes in the maze and the sdd screen at least i believe that's correct and i think with that this should all work now there's a chance i made a mistake and we'll have to fix that but for now let's run the code and give this a test so let's make the terminal nice and large let's run this and notice it just instantly found the path and we get that now of course we want to be able to visualize this so we can make it a bit slower and the way we can do that is by using this time module so if i go here where my refresh is pretty much anywhere in here what i can do is time dot sleep and i can sleep for like 0.2 seconds 0.5 seconds whatever you want just so that you have some time to kind of process what's going on so let's leave for 0.2 let's make this larger and let's run this and notice that now you can watch the breadth first search algorithm in action and see every path that is currently considering and there you go we just found the finished path and the algorithm is done all right so there you go that project is now finished was a bit longer than i was expecting but obviously i wanted to explain this as best as i could let's walk through it step by step to make sure everything is clear and then we'll move on to the next one so we start here with main we've already talked about curses we know how this works and we call find path now we call find path we pass the maze and the standard screen and inside of find path the first thing that we need to do is determine where the start position is in our maze because we're using coordinates right and we're trying to use that so we need to know the starting position so instead of find start we just loop through the maze so we go through all the rows we go through all of the values in each row and if we find a value that's equal to the start symbol which i'm denoting as o then we're just going to return whatever position that's it and that way i can now very easily change the start position put it anywhere in the maze and this will adapt and in fact let's just do that by putting an o up here okay let's save and let's make sure there is a path uh yes that's fine okay continuing we then have our queue so we define our first and first out data structure and we add our starting position and the starting pause now i'll give you guys kind of a challenge here if you want try to do it by just having the path and not having the starting position there's a way to do that again i just haven't done that in this video then i have my visited set keeping track of all the elements i visited i'm saying while the queue is not empty what i'm going to do is get the current position and the path from whatever's at the front of the queue i'm then going to get the row and the column of the current position and i'm going to draw out the maze with whatever current path i'm on whatever one i've got to at this point i'm going to check if i'm at the end if i am at the end then i can return the path because i found that end node if i've not then i need to continue looking for the endnote right so i'm going to say neighbors equal to find neighbors maze row and call i'm going to loop through all of the different valid neighbor positions i'm going to check if they're visited and i'm going to check if they're an obstacle if they are i'll skip them if they're not i need to add them to the path and add them into the queue because we need to check now if they're the end node and if we need to expand from them right that's how this algorithm works then i add them to the visited set and this continues until we either find the path or we visit every single node that we have so every single one of them has been added into visited that's not an obstacle and we can't find a path so this will end if either there's no path or you find the path that's how that works all right find neighbors pretty straightforward forward gives us the valve neighbors and then print maze i won't explain that we walked through that already okay so that is that project let's just run this one more time for a sanity check that it's going to work when we change the starting node and let's see what the shortest path is here okay and there we go we have found the shortest path of course make this maze as large as you would like and you can see you know how that works all right so with that said that is project number one hopefully you guys enjoyed that definitely actually the most complicated one that we have now that i think of it now though let's move on to project number two all right so now we're moving on to project number two which as i said is going to be the nba project where we're grabbing basketball data there's a ton of data you can get if you're into basketball this is going to be really cool for you and even if you're not you'll learn how to work with an api how to process some data external data in an application kind of display that in some format send a request etc so what i'm on right now is a link it's data.nba.net prod v1 today.json this is a free api and what this gives you is links to all of the pieces of data that you have access to for free so it's kind of weird but this link gives you the links that you're going to look at to get specific data so for example if i search for let's zoom in oh that's a lot easier to see so for example we have this calendar okay so this says calendar and it's prod v1 calendar.json what that means is i copy what's called the endpoint and i just tack that on oops that's not what i meant to click to the end of this year so i paste that so now i'm going to calendar.json and if i hit that it gives me a calendar that contains i guess some calendar data i don't know exactly what this is doing or what this is showing but the point is that it gives you a bunch of links for all different types of stats that you can get so you can get like all-star roster league config standings league div standings whatever you want you can have a look at and you can grab that data you first go to this link and then it gives you the link to that data that's how that works okay hopefully that makes a bit of sense i'm going to leave that open now though we want to essentially grab the data that that link's providing from code and the way we're going to do that is we're going to send a request to that link it's going to return to us something called json which stands for javascript object notation i have an entire video on json on this channel but pretty much this is a python dictionary that's the way you can think of it and we can access the different key value pairs grab the links for data sources that we want and then send a request to those links and get data from that so it's kind of a two-step process we send a request to that url it gives us some data on the different links we then send a request to one of those links and get the data that we actually want and a request is essentially our computer requesting information from a remote server we're going to send something called a get request which as it says requests information there's other type of requests like post requests put requests patch delete requests that do other types of stuff but that's kind of beyond the scope of this tutorial however before we can do all of that we need to go and install the request module now the way you do that is similar to how we installed curses before go to your terminal and type pip install and then request like that again if that doesn't work try pip three if that doesn't work try python hyphen m pip install otherwise you can watch those two videos that will be linked in the description and it should show you how to fix that but you need the request module installed if you're on windows and mac you do need to install this unlike curses it will not be installed by default and again just reminder try pip 3 in case that doesn't work and then you can try python hyphen m pip or python 3 hyphen anyways i have this installed so that's all working for me and let's now go in here and start writing some code so i'm going to say from requests import and we're going to import get and i'm going to say from and this is going to be print import and we're going to import the pretty printer like that i'll show you the pretty printer kind of a cool thing in python it's built in and it gives us a nice formatted output for json data then what we're going to do is have our base underscore url which is going to be equal to https colon slash slash and this is going to be data dot nba dot and then net okay this is the base url and then the endpoint is going to be the specific data that we want so i'm going to say all json is equal to and this is going to be slash prod slash v1 slash today.json and today.json is the original one we're looking at that gives us all those links now though we want to send a request to this link so the way we do that is we say our response because every time you send a request you get a response is equal to and we're going to say get we're going to say base url we're going to append this or we're going to concatenate this to all json so we'll have you know the full url built out and then we're going to have a look at what's known as the json of this so whenever you send a request here you have access to json again think of this as a python dictionary and now we can print all of that out however you're going to see if i print this with a print statement so let's try this here if i print the response then we get kind of this garbage in the terminal that's just impossible to read so instead of printing it using a regular print statement we're going to use a pretty printer really cool thing if you haven't seen this so to do this we're going to say printer is equal to pretty printer make sure you add your parentheses and then we can use the printer by saying printer dot and then p print so pretty print and we're going to print the response and really we should call this data because we're grabbing the data the json associated with so let's say data okay so let's run this and now notice that i get much nicer formatting here everything's kind of separated on different lines that's what we had before this is what we have now when we use the pretty printer so inside of here we can see that we have a bunch of links now these links give us access to all of these different things right so game book lead tracker mini box score whatever data it is that you want you pretty much get all of it so what we want is the current scoreboard because we want to get information about games that are currently going on or that are going to happen today so what i'm going to do is try to grab this current scoreboard key and get the link that's associated with it so the way i do this first of all is i need to access the links key right so that i can start looking at this dictionary then i need to access the let's have a look the current scoreboard so we'll do it step by step let me just close this terminal and let's do this so we have our data now we want to say links is equal to data at and then links and let's print this out so printer dot p print and then links okay so let's have a look at that and now when i print out the links we just get all of the links in here and now the link that i want is the current scoreboard so i'm going to grab the current scoreboard link so i'm going to copy that there and now i'm going to say that my scoreboard is equal to and this is going to be links and then we're going to paste in here the current scoreboard so now let's have a look at the scoreboard okay let's run this and notice here that this is the url so prod v1 2022 whatever scoreboard.json now that i have that as it was saying i need to send a request to this endpoint the endpoint is the slash after the base of the url so that i can get bad information so now that we have that we have the scoreboard url let's make a function here and let's say get underscore scoreboard okay let's put uh let's put this inside of here and we're just going to separate things to make it a bit cleaner i'm going to say define get underscore links okay and inside of here we're just going to return links and now what i'm going to do is rather than links i'm going to say get underscore links like that and then i'm going to access the current scoreboard so i've just made it so i can reuse this function here so it gets gets me all of the links sorry so now i get the links access the specific link that i want and now i'm going to send a request to that link so i'm going to say get and we're going to say base url and this time we're going to say plus scoreboard okay and we'll say that this is response and actually i guess we could just do data is equal to and then we can get the json and then again we can use the printer so printer.pprint and we can print out our data okay so now all we need to do is call the getscoreboard function so let's call that and what's going to happen now is it's first going to send a request to what was it to the old json url it's going to give us the links we're going to access current scoreboard then we're going to send a request to that get the data and print it out so let's clear this and let's run and notice that i get all kinds of data here right and this is kind of hard to read like there's a lot of stuff so it's difficult to look at what the keys are so what i like to do here to actually see how i can kind of traverse this data is i like to print out the keys because remember this is a python dictionary so since it's a dictionary what i can do is just grab all of the keys of the dictionary and then i can see what data i want to grab next so if i run this now and we scroll to the bottom notice that my keys are internal numb games and games so i probably want to access the games so let's clear that and just access the games for now so now i can go json and games and now let's have a look at what this data looks like so let's run this and notice again i get a bunch of stuff so now i probably want to have a look at uh the keys of this so let's say data dot keys okay let's run this and what does it say here a list object has no attribute keys okay so what that means sorry guys is that the games is a list of all of the different games so what i can do instead now is i can say 4 game in games because this is a list we just found that out and now i can simply print the game dot keys and just break and what that means is i'm just going to do this one time so i'm just grabbing you know the first game and printing out all of the keys associated with the game so i can view that so now let's run this okay and name games is not defined oh my apologies let's call this games not data okay let's clear the screen let's run and now i get all of these keys okay all of this information i have access to for every individual game and the games are games that are going to be happening today uh that that's what the games are so now i got to pick what information i want to display because i probably want to print out you know who's playing what the score of the game is uh you know what time is it is in the game that's the stuff i had before at least so i'll probably do the same here so we can kind of pick what we want but we have v team and we have h team those stand out to me as the visitor team and the home team we also have period and we should have clock so i think those are the four ones i want for right now so let's grab those so we're going to say home underscore team is equal to and this is going to be game at and then this is going to be h team right because that's the key we want to access and then i'm going to say i guess away team is equal to and not a team but v team and we can print out i guess the clock and the period as well we'll say clock is equal to game clock and we'll say period is equal to game period and now we don't need to use the pretty printer because we're just going to print these uh on one line and we will print an f string and we can say home team versus and then away team and then we might as well just print the clock and the period as well just have a look at what those are and then we'll continue from there okay clock and period so if you're unfamiliar with this in f string allows you to embed expressions directly inside of a string when they're in curly braces so i mean this is pretty intuitive but an f string only works in 3.6 plus so python 3.6 plus so i'm just putting the home team versus away team comma clock comma period just so i can see what those look like and i'm going to do that for every single game so just to separate these a bit then let's add a bit bit of a separator just so that we can see uh the difference in games that's clear let's run the code and notice that i get all of this data okay so for each team i have a team id a try code wins losses series wins siri losses i get a bunch of data right and then the score of those teams because i guess some of these games are actually happening right now so what i can do is print out the try code if i want to see you know the actual name of the team and then i could print out this score as well so let's try doing that now so for our home team that's fine now we're going to say home team and inside of here we'll say try code and for the away team same thing we'll say try code just to clean it up a little bit so let's run this now and see what we're getting in terms of the difference so now we notice we actually get the uh abbreviations for the team then we're looking at what was the next thing we're printing the clock okay so 44.1 is what's on the clock for this one and then we have the period and it says what the current period is for the games that are currently going on okay nice so let's get rid of clock and period so those are the teams next thing that i want to print is the score of each team right so i'm going to print f and then this is going to be the home team and not the tri-code but the score this is the score in the current game and then we'll print the away team and the score as well and so now we get the teams that are playing as well as their scores in the game and then we can print the clock and period after that okay so let's clear let's run and notice here that we get i guess what is that chicago i don't know what these teams are but something 17 19 31 22 14 19 etc great and then here we're just getting a hyphen because these teams are not yet playing and zero zero i guess those guys just started okay continuing now we want to print the clock as well as the period so let's say print and we'll print f and i guess we'll go we should probably do should we do clock or period first let's go clock period and then we'll see what keys we have on those so let's run this okay and we get current so that's what we want to access for the period and then it looks like the clock just gives us a value so that's fine we can just grab the clock itself okay so let's go clock and then period and we want to get current like that okay let's run this again and now notice that we get 32.0 and then that's period one and then 142 period one 409 period one you guys can format this better i'm just giving you the basics obviously okay so that's what we wanted for getting the scoreboard now we also can get a bunch of other data and like i showed you we can get stats for the teams so they're points per game i guess their points allowed per game there's all these different stats i'm not a huge basketball guy i just thought this be cool but let's write one now that gives us the teams and we can view their stats and like i had before we will rank them based on points per game so to do this one we're going to want to look at get links again so let's not call get scoreboard let's call get links okay let's clear this and let's run the code and let's see what links we have for stats i didn't print sorry i need to print this let's print get links and actually we should probably use the printer to do this so printer.p print okay so let's have a look here and these are all the ones that we have now let me remember which one i was using i think it was league team stats yeah league team stats leaders okay this is the one that i want to get stats of course there's a lot of other ones so i'm going to copy that key and now we're going to write another function i'm going to call get underscore stats okay that's fine for now and i'm going to say stats is equal to and then this is going to be get underscore links and we're going to reference this right here the league team stats leaders okay then we will send a request so let's just copy this and rather than games i guess this is going to be teams and we'll have the base url plus the stats and then json and then we want to print out all those keys so we see what we have access to so let me take this in here and rather than get links we'll print out should really call this data so let's call this data okay now let's call getstats so get stats like that okay let's run this and let's see what we get and notice we get a lot of information okay i want to print out just the keys that's my bad i meant to do that so let's do dot keys and let's see what we actually get for that okay scroll down and we get league okay so that's the only key so let's access league so let's just put league here there's internal as well but we don't care about that that's some other data let's see what keys are associated with league so let's run that we have standard africa sacramento vegas utah i'm assuming we'd want just the standard league okay so let's grab standard okay let's run that and for the keys we have season year preseason regular season playoffs i'm assuming we probably want regular season but again you can change this if you want so let's now grab regular season let's print that and we only have the key teams okay so now let's grab teams and then this will be called teams let's see if there's any keys for teams so let's run this now and it says oh data is not defined so let's change this to the teams okay and rerun list object has an attribute keys okay so it's a list meaning that if we want to grab the keys for each team we can just do something like teams zero dot keys and if we have a look at this now we get all of these different pieces of information for the stats of each team so i can get the nickname the name the team id team code abbreviation min fgp tpp i don't know what a bunch of these mean but i know ppg is points per game so that's the one that we're going to use because i know what that means anyways those are the different keys we can get for each team so now we can loop through every single team and we can print out information for them so let's do that so let's say for team in teams and then we'll grab the names say name is equal to team name what else do we want i guess we'll get the id we'll say the id actually no i don't need the id we'll just go with name and i suppose we get the nickname as well nickname is equal to team nickname and let's go ppg is equal to team and then ppg okay and then let's just print for now we'll do an f string and we'll print the name the nickname and the points per game okay so let's have a look at this now let's clear and let's run and there we go okay so apparently we have a bunch of teams here that are not in the regular season or don't have a rank or something so we'll have to filter those out we'll do that in a second but for now we have the all the teams right so we have the name we have the nickname we can choose which one we want to use and then we get average and we get rank for points per game so this is where it gets a little bit more complicated i want to display these teams in the order of their rank right so i want rank 1 to be at the top and i guess rank 29 30 whatever to be at the very bottom but i also need to filter out these guys because i guess they're a bunch of teams that we don't really need in our data set i don't know why those are being returned to us so the first thing i'm going to do here is i'm going to filter all of the teams that have a name of team so the way i'm going to do that say teams is equal to filter i'm going to say lambda x i'll explain this in a second don't worry and i'm going to say x at team like this does not equal and then we're going to say team because anyone that had the name team was like a blank team that we didn't really need and then we'll do this on teams okay so what the filter function does is it will run a function against every single element in our teams in this case that's what we're passing and if the function returns true it will keep the element if the function returns false it will remove it so this is an anonymous function that's what a lambda function is this is our parameter essentially the function called x so i write lambda x and then i say colon x team does not equal team i'm saying let's grab whatever the team key is of every one of our dictionaries and if it is not equal to team then we will return true so like this expression returns true or false so if the function returns true we keep the element if it turns false we don't keep it hopefully that makes a bit of sense i can't really explain it more than that however when you have a filter function here it returns a filter object not a list so you just need to convert it to a list okay so now that we have that let's just run this again and you should see that all those kind of empty teams will be removed so let's run it and key error team oh sorry not team this needs to be name okay let's fix that let's rerun and there we go so now all the ones that had just team are removed now what we need to do though is sort our teams by their rank in their points per game so to do this we're going to say teams dot sort and we're going to say key is equal to and again we're going to put an anonymous anonymous function we're going to say lambda x and then this is going to be x p per g p g for points per game and then this is going to be rank like that and this is going to be int now this is going to sort in what is it ascending order so we're going to get rank 1 first rank 30th 30th last now the reason i'm doing this is because points per game is a dictionary containing the rank and the average i want the rank so i'm grabbing points per game rank for every team converting that to an int so then we sort it as an integer not as a string because notice these are string values right and that's that's how this works you can pass this key function and that's what you're going to sort every element by when you're sorting now let's run this and notice that we're going to have all the ranks in the correct order now of course i don't want to display you know this weird dictionary i'd prefer to display the following ppg and then avg like that for the average so now if we rerun this we get the average number of points that each team scores per game and we get them ranked from 1 to 30th lastly if one other touch here we could do the following i'm going to say i plus 1 dot and i'm going to say 4i comma team in enumerate teams and now it will say the rank beside each team so let's do that now and let's run and notice that now i get my rank and i also could have just done ppg at rank as opposed to doing i plus one but this works as well okay so that is going to wrap up this project let me quickly walk you through it so we start by defining the url that we're going to send our request to we had to import and install the request module we set up our pretty printer we have get links which gives us the link we're going to send our request to to get specific data we then have this function to get the scoreboard that is the games that are currently running uh and so we have current scoreboard we get the json we grab all the games associated with that for every single game because this is now a list we loop through we get the home team uh the visiting team the clock and the period and then we display this data and of course you can display this nicer you can get some more info i thought this was good enough for the tutorial then we have our get stats function inget stats is going to grab the league team stats leaders it's going to do the filtering it's going to do the sorting and it's going to go through and display all of the teams ranked by points per game so i just called get stats to run that you can call get scoreboard to run this you can run both of them you could write a little program that asks the user which one they want to run i'll leave that to you guys now we're going to move on to project number three all right so now we're moving on to project number three which is a currency converter now for this project we need to go to this website free.currencyconverterapi.com to get an api key that we can use with this api now this is a free api as it says in the name you are rate limited which means you have two requests or sorry 100 requests per hour you can do a maximum of two currency pairs when you're getting the exchange rates per request and you're allowed to go back one year in history now you can pay for a premium version of this obviously we don't need to do that for this tutorial though we just want to get the exchange rate data now this data is updated i believe every 60 minutes when you're on the free version i think if you go to the paid version you get it every minute or 60 seconds or something like that again it doesn't really matter for this tutorial anyways go to the website link in the description click on get your free api key enter your email it will send you the api key but make sure you verify your email because if you don't do that then it won't activate your api key anyways i already have my key and you don't want to share this with anyone of course it's the video i don't really care i'm going to have to share it with you but what i'm going to do is make a variable here in a new file called api key and i'm going to paste in my key which i'm just going to grab from my other screen now while we're at it we're going to paste in another variable here called base url now this is going to be equal to a different url than you just saw it is free.kerconv.com so that's the url we want to be sending our requests to for this project to get the up-to-date currencies right okay then what we're going to do is say from request import get now if you were not following along with the previous project which is fine what you need to do is install requests so pip install request like that you can also try pip three install request and if that doesn't work try python hyphen m pip install lastly you can try python 3 hyphen m pip install if that doesn't work there is two videos linked in the description that will show you how to fix that command one for mac and one for windows okay now that we have that we're just going to import the pretty printer as well just so we can use that again we use this in the last videos sorry so from print not last video last project we are going to import the pretty printer okay so this api works a little bit differently than the last one we need to send what's known as some query parameters when we want to get some specific data and the first thing i'm going to do here is show you how we can get a list of all of the currencies and then how we display those currencies then we can get the exchange rates and all of that stuff for now though i'm going to set up a printer so i'm going to say printer is equal to pretty printer again if you weren't following with the last video then what this allows you to do is get some nicely formatted output for your json the json is essentially a python dictionary that's going to be returned to you when you send a request to a url okay so now that we have that let's get the currencies the way that we get the currencies is we say endpoint is equal to and then i need to copy this in so let me just copy this from other screen and this is going to be api v7 currencies and then question mark now this starts what's known as a query parameter a query parameter is essentially a parameter some data that you're going to send to the url and in this case what we need to add is our api key so we're using an f string available in python 3.6 and above we're embedding the api key directly in here and that's what's going to allow this to work if you don't have the api key then this won't be validated and you won't be able to send a request now let's actually do this in a function so i'm going to say define get currencies like that and we're going to say this is our endpoint and we're going to say that our url is equal to our base url plus our endpoint and then we're going to say that our data is equal to and this is going to be get we're going to send a get http request to this endpoint so we're going to say get url and then dot json like that and for now we're going to say printer dot p print standing for pretty print and we're going to print out the data and we'll just call the get currencies function okay so that's all we need for now to look at the currencies so let's run this uh and no module name request okay it needs to be request plural my apologies let's run this again and notice that we get all of our different currencies okay so when i have a look at what we're getting returned here we get a key that says results now again this is just a python dictionary that's the way you can think of it really it's json but we it gets read in as python dictionary so i can access this results key which is then going to give me a dictionary that has keys for every single currency and then the value of all those currencies is going to have an id a currency name and optionally a currency symbol so i want to be able to display that data to the screen so what i'm going to do here is from this function i'm going to return the data and then this is going to be at results like that and i'm also going to sort all of the results before i return them so i'm going to say that this is results okay i'm then going to say here data.sort so actually what i'm going to do here on a new line is i'm going to say data is equal to and then this is going to be the list of data dot items the reason i'm doing this is because data right now after i get results is going to be a dictionary as we can see i don't want it to be a dictionary i want it to be a list and the reason i want to be a list is so that i can sort it by the different currency names so i'm going to convert it into a list by getting all of the items the items will give me tuples that contain the key so that be the currency and then the dictionary associated with each currency then i'm going to sort this by default it will sort by the first item in python so i'll sort by the currency name and then i'm going to return my data like that so now i can say down here data is equal to get currencies and i can say printer.pprint and data and hopefully this will look a little bit cleaner now and it should be sorted so let's clear the screen let's run this and then notice we're getting tuples here right where it contains the currency i guess you could call it ticker and then the information associated with that currency and it's in alphabetical order based on i'm going to call it the ticker or the id whatever for the for the currency so now what we want to do is write a function that can display this in a much nicer format so we're going to say define print underscore currencies we're going to take in our currencies and we're going to loop through them and print them out so we have data there that's fine so now we're going to say for currency and then this will be well we should probably spell currency correctly so for currency in currencies uh we are going to grab the name of the currency so if we look here we see we have currency name we want the id and then potentially the currency symbol but not all of them have it so we're gonna have to write some code for that okay so let's say name is equal to and then it's going to be currency and i believe it's currency name and then we want the id so we're just going to say underscore id i don't want to use id because that's a built-in keyword in python and this is going to be currency actually no it's just going to be id okay and then the symbol will be so symbol is going to be equal to currency and then this is going to be dot get and we're going to try to get the currency symbol like that and if it does not return a symbol then we're just going to get an empty string so when you use dot get that will try to find this key in the dictionary if it exists it gives you the value otherwise it gives you the default value which is an empty string which i'm passing right here okay now that we have that we can print this out so we're just going to print an f string again and we'll have the id first because that's going to be the three letter symbol then we will have the name and then optionally we will have the symbol there okay hopefully that makes sense let's now call this so print underscore currencies let's pass our data and let's see what our output looks like here after i clear the screen okay let's run it and tuple indices must be in integers or slices not string my apologies guys so we're going to say for currency uh let's do this for name comma currency and currencies the reason we have to do this is because this is a list of tuples i forgot about that so now if i run this it should be good and there we go now we fix the problem so we can see that we get our little ticker currency if there's a symbol it will display the symbol if there's not a symbol then well we don't show it okay nice so now that we have that we want to write something that can get an exchange rate so i'm going to say define exchange underscore rate i guess like that and for the exchange rate we need two currencies that we want to get the rate for right so we're going to say currency one and currency two now these currencies are going to be the ids of the currencies we listed out so like cad usd whatever like the ids is what we need to pass to this function for this to work now the end point for this i'm just going to copy it in here is going to be the following endpoint is api v7 convert question mark q okay for query right we're going to say is equal to currency one and then notice this is underscore currency two so a little bit weird how this works but for this specific url you pass the first currency underscore the second currency and that's the pairing that you want to get information about now you can pass multiple pairings like i could do usd underscore cad here and it would give me both of the results now we're just going to do one at a time and then you say and compact equals ultra and api key equals api when you do compact ultra it gives you the most compact version where it's just going to give you the exchange rate and then of course you need your api key as well so we're going to embed that in this way hopefully that makes sense now we're going to send a request here so we're going to say url is equal to base url plus endpoint we're going to say response is equal to get url.json and sorry not.json we're going to say data data is equal to response.json and let's now just print this out so let's go printer.pprint and we're going to have to handle potential errors that can occur here if you pass invalid currencies so for now though we can comment this out and we can just call the exchange rate and let's pass this with usd and cad and it will give us the exchange rate between usd and cad not from cad to usd although i guess you could go both ways okay let's run this and notice that we get usd cad and it's 1.259025 looks about right and if we pass the second pairing it would have given us this key for whatever the second pairing was and then the value hopefully that is clear but that's what we're getting from that so what we need to do now is essentially check to make sure that this actually worked and the reason i'm saying that is because if i pass something like you know random string here and i run this notice that my data is empty because there was no pairing with that name so what i'm going to do here is i'm going to say if the len of data is equal to 0 then i'm just going to print out invalid currencies i guess we could just do i guess that doesn't really work we'll do invalid currencies and then we'll just return otherwise though we want to get the pairing and we're only going to have one pairing here in here one pairing here and here okay interesting how i said that but we're going to return data and then this is going to be dot items actually not items dot values and we're going to convert this to a list first of all because when you get the values it gives you kind of this special object and then we'll grab the zero with value and we can actually that's already a float so we don't need to convert that okay so let's now say rate is equal to that and then print rate okay and then this will be usd so what i'm doing is i have my endpoint right i have my url i'm getting my response i'm getting the data associated with that i'm saying if the line of data is is zero then print invalid currencies i also realized that we can just clean this up so let's just make this data like that now if the length is 0 that means well we didn't have a valid pairing so we print that out and we return otherwise though we're going to convert all of the values it's just going to be one value really to a list we're going to grab the zeroth element which will just give us whatever the rate is and then we can print that out here okay so let's run this and notice we get 1.259025 nice okay so now that we have gotten that uh maybe i want to add some output in here actually that tells me a bit nicer what the rate is so let's do that i'm going to say uh should not print i'll say rate is equal to this and we will return the rates but before i return the rate i'm just going to print out and we'll do an f string here and we will say that what is this going to be currency one okay to currency two is equal to rate i don't know if this is exactly the way that i would write this but i think that's fine like usd2 cad for example is this rate yeah you know what for now we're just going to go this uh we'll say is equal to that that's fine for me okay so let's just run this one more time and notice that i get usd to cad at one two five nine zero two five okay perfect that works for me now that we have that we're going to write one more that is going to take a dollar value and convert that dollar value uh to the other currency so we're going to utilize this function but i'm going to say define and then this is going to be i don't even know what i would call this here i guess convert and convert will take currency 1 currency 2 and an amount now in here we're going to say the rate is equal to exchange rate of currency 1 currency two i'm going to say if the rate is none so if we're returning from here meaning we had an invalid currency then we're just going to return we don't need to do anything otherwise though we're going to try to convert the amount that was passed here to a float so i'm going to say try and then i'm going to say amount is equal to float amount i'm going to say accept and then what i will do is say print invalid amount so if you pass like a string amount for example here something that i can't convert to a float i say invalid amount and then just return so that that way we don't have any issue when we try to do a multiplication which you're going to see in a second okay so we try that that doesn't work we say invalid amount otherwise we're going to say converted underscore amount is equal to and this is going to be rate multiplied by whatever the amount is right and then what we can do is print with an f string here and we can say uh let's say amount and then currency one and let's just say is equal to and then we'll say converted amount currency too okay and then just because we might as well just return the converted amount okay so now we have four functions get currencies print currencies exchange rate and convert so now what we want to do essentially is just write a program that keeps asking the user for a command and allows them to quit but they can type in a command and do whatever they want with the currency converter so i'm going to say define main inside of here i'm going to say currencies is equal to get currencies okay uh and then i will just print out like a little welcome message so welcome to the currency converter okay exclamation point and then we'll print the different commands so i'll say list lists the different currencies okay we'll say print and then this is going to be convert convert from one currency to another okay and then print we'll just call this rate get the exchange rate of two currencies okay now that we have that let's just do one empty print statement just there's a separator and that will say while true and we're going to ask them to enter a command so we'll say that command is equal to input and we'll say enter a command and we'll just say that they can type q to quit like that okay so now in here we're going to say actually first let's convert this to dot lower just so that we can check um even if they type something uppercase we'll still get the command so first we'll check if they quit so if command is equal to q uh we need two equal signs here then we can just break out of the while loop so let's break we'll say l if the command is equal to list then simple enough we can just print the currencies okay so print currencies like that that's all we need for list we'll say l if the command is equal to and then i guess we can handle convert then we need to ask the user for two currencies as well as an amount so we're going to get currency currency1 which is equal to input uh enter a currency name okay actually let's say id all right but we'll convert that ah actually we're going to convert it to dot upper not lower now let's copy this again so let's say that's currency two actually we'll say enter a base currency id enter a currency to convert to uh maybe we'll just do this i think this reads a bit better enter base currency enter currency to convert to and then we want one for amount so i guess i could have just written this from scratch we'll do this in between i'll say amount is equal to input enter an amount and then we'll say in and this will be in the base currency so we can say in and then currency one okay and well we don't really need to convert that to dot upper because it's going to be at a value once they do all of that then we can call our function which is convert and we can pass our values so we can say convert like that and we pass currency one currency two and amount okay lastly so we say l if the command is equal to rate then we just want to get the two currencies so let's grab currency one and currency two and let us call exchange rate so exchange rate of currency 1 and currency 2 and else we'll say print unrecognized command okay i think that is all we need now we need to of course call the main function okay let me zoom out a little bit just you guys can read a bit more so we have our main function we're getting all the currencies we're just printing a little welcome message and then we're running a while loop and we're saying while true we're going to ask them to enter a command cue to quit of course if they enter q we will break so we'll quit if they enter list we'll print the currencies if they enter convert we'll ask them for the information we need then we will convert and convert here we'll do everything we need and print out the output then we have our rate where we get our two currencies and same thing we get the exchange rate and that will handle printing out the output as well so i believe that's it let's run this though and test it out so run and welcome to the currency converter let's zoom in a bit here list convert and rate enter command so let's try list okay there we go list everything out let's try to do the rate and let's do usd and czar okay and gives us 14.944 okay let's now try was it exchange knows convert convert enter a base currency let's go usd uh enter an amount in usd let's go 1 000. let's go xof which is one i just saw up here west african cfa franc okay and there we go gives us the conversion nice now if i hit q i can quit and that is all the program needs to do all right so with that said i think i'm going to start wrapping up the video here that was three mini python projects for intermediate programmers not overly complicated not super simple you know short period of time but also allows you to create something kind of cool i think these programs are somewhat useful and definitely a good kind of introductory project something you can build on to and hopefully a good learning experience for you guys to understand how api works maybe you learned about the pretty printer or maybe you learned about the curses module whatever hopefully i give you guys some value in this video with that said i will give one last plug to programming expert if you guys want to get better at programming that is my programming course you can check it out from the link in the description use discount code tim if you guys enjoyed this video make sure to leave a like subscribe the channel and i will see you in another one [Music] you
Info
Channel: Tech With Tim
Views: 158,989
Rating: undefined out of 5
Keywords: 3 mini python projects for intermediates, python, python projects for intermediates, python project demos, how to program shortest path finder, how to code shortest path finder, how to program NBA stats and scores, how to code NBA stats and scores, how to program a currency converter, how to code a currency converter, programming expert, altium designer, fix pip, how to fix pip, json, what is json, good python programming projects, good coding projects to try
Id: txKBWtvV99Y
Channel Id: undefined
Length: 85min 15sec (5115 seconds)
Published: Fri Mar 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.