 Hey guys and welcome to part two of creating a library system using Python. So before we start this tutorial I just want to make sure that you guys are aware that this tutorial is going to be for pure beginners So the stuff that we're going to be doing in this tutorial is going to be pretty basic And we're not actually going to go through the full scope of creating a library system But just a basic aspect of it. So if you haven't already watched the previous tutorial I highly recommend you do because in part one what we did was scraping the data from a scraperable website using requests and proxies in order to basically have the data related to books, the availability and their prices that we're going to be using in this tutorial. So the last tutorial was sponsored by IPRoyal who have kindly also sponsored today's tutorial. IPRoyal is a proxy service provider providing safe private and unrestricted access to online information. With a pool of over 2 million plus reliable IPs IPRoyal allows clients to use a proxy server as an intermediary between their devices and the web which allows clients to maintain their privacy and use resources they can't access directly due to geo restrictions etc. IPRoyal's data center proxies can serve as a great product for businesses or users looking for premium high-speed anonymous private proxies which usually have unlimited bandwidth and no extra charges. For data scraping though I would recommend using IPRoyal's residential proxies as they not only allow anonymity when scraping websites which can help avoiding getting weight limited but also let you select the geo location of the proxy either through the dashboard or by making minor changes in your code. Lastly IPRoyal handles automatic IP rotation which not only allows easy integration within your code but they also provide the option to use static IPs in case that you decide to keep an IP for a longer duration of time. In this video IPRoyal has given me access to their discount codes which will give you a straight 30% discount on your Royal Residential Proxies. The discount code is your hand 30 and can be used to buy the Royal Residential Proxies. So guys please make sure to grab your discount codes and make your purchases for Royal Residential Proxies today from IPRoyal or you'll be missing out on a great deal. So now that you know a bit more about IPRoyal what we're going to be doing is hopping right into the code. So what we have here is a .py file that we're going to be coding in. So what we want to do first is we're going to import a library called pandas which is what we're going to be using to basically read our data. So import pandas as pd and we also want to import time because we're going to be using time to basically call some delays in our program make it look a bit more realistic and fun than usual. So first things first what we want to do is if you're working on your desktop for example which I am you want to move your database file that we basically scraped in the last tutorial onto the same folder as your script. So that's what I'm going to be doing now if you bear with me once again just going to move my database file to the desktop and once that's done what you want to do is create a new variable called data and we're going to use pandas to read in that CSV file so make sure to mention the exact file name including the power extension and what we can do is just print the data to make sure that we're actually loading in this properly. So open cmd here and then I'm going to activate my environment because I've got different environments for different projects but if you've installed audio packages like pandas and time on the same like default environment that's okay you don't need to activate any environment. So to run the script I have to first navigate to the folder where my script is so mine is on desktop and then just type in Python and the name of the script so Python library system main.py. As you can see the first part of the code ran successfully because it's printing out the CSV file as a nice data frame so we've got four different columns so we've got the book ID, book image URL, book price, book availability and so on. So the book ID was actually manually created by me because the website we didn't scrape the book ID from so what you want to do in Excel is just create a new column called book ID and then just start typing in numbers from one until all the way to the end because we're going to be needing that in our code. So I shouldn't have closed that but anyway what you want to do next is create a variable called balance so this is going to be your account balance so how much money you currently have in order to purchase books. We'll set that up to something reasonable like 40 pounds and then we'll move on to the rest so the next thing is creating a menu function which will basically print out the different options that the user has access to on the main menu so it's just a bunch of print statements the first one will be welcome to the library you can give your library a name but I'm not as creative so I'm just going to call it welcome to the library and then we want to type in the different options so first option is going to be purchase a book and the second option is going to be exit so like I said the tutorial is going to be pretty simple and we're just going to be having two functions one being purchasing a book another one being exiting the library so if you wanted to if you like want to practice your code and skills after the finish of this tutorial you could add another option called returning the book which when you return the book the library will return the money that you paid for the book so that's up to you guys but in this tutorial we're just covering purchasing a book as a functionality so the next function we're going to need is quite an important function it's basically going to print out all the books and their information to the user so we're going to do view books as the function name and then what we're going to do is basically loop through the data frame or the CSV file that we loaded in with pandas so we're going to loop through each row of it so for I and range 0, length of data what we want to do is create a new variable called row and row is going to be data.iLockI iLock will basically help you locate each row using the index so it's row start with 0 and ends with whatever the last index of the data frame is so now that we've got the whole row what you want to do is grab the different elements of it so we need the ID so I'm going to do ID equals row and that's going to be book ID so you need to put the column names in here then we want the title as well so title is going to be row book title that's the second column then price which is going to be row book price just make sure you have these exact same column names in your CSV otherwise you're going to encounter an error availability and mine is called book availability okay now the last thing we want to do is actually just print this in a nice format so that all the books can be like printed out to the user and the IDs can be printed out as well so they can select what book they want to purchase for example so I'm going to do print strid so that's the book ID plus dot title and then we're going to print out the title then we want to show the price of the book so price add a pound sign or dollar sign whichever you prefer and strprice and then lastly the availability so the availability variable okay so that should be our view books function pretty much ready let's test it out by running it so we'll run the menu function first and then we'll run the we'll do a time dot sleep here of like let's say five seconds so we have enough time to see it and then we'll do view books let me do the same process again in cmd because I accidentally closed the windows up to now do the whole thing again okay here we go the first part is running so the menu works it's showing hopefully and then as you can see our view books function is pretty much working as intended as well it's pretty got the ID of the book the title the price and availability as well which is perfect because the user can basically just call for it and find the ID of the book they want to purchase cool so this time I'm not gonna make the rookie mistake of closing command prompt okay just gonna do CLS to clear the console again nice now the next thing you want to do is we can get rid of this because we were just testing those functions we want to create an infinite loop which is only going to be broken when the user presses the number two to basically exit our program so first things first we want to display the menu to the user then we want to grab the input from the user based on the menu so we're gonna grab an integer input and create a new line and so please enter a number funding the options then we want to basically print the option that the user selected back to him so you selected option and then we'll do comma menu option menu input so that will basically show the user what option they selected just to cross check with them and then we can start with the logic so we'll say if menu input is equal to three we said sorry two we said two would be exit so if it's two then we simply print exiting and then we time got sleep that's a three seconds and then just break through the loop and they'll pretty much break through our main loop and end the program then we want to do an elef here so it was a new input equals one which is basically purchasing a book this is where we have to create another loop because there can be a few things that happen when the user purchases a book for example they may want to purchase multiple books and then return to main menu so we don't want to redirect them straight away to the main menu unless they want to so we do another loop so well and then first things first review the books so let's do this for now and just test that program so we're going to test the existing functionality and we're also going to test the input menu and we can also add an else in here to say if the user enters anything apart from these two options just say something like something like that so let's test it out get my cn command prompt up here the command let's do let's say five and it says you selected five invalid option please select it please try again let's exit and they'll exit in I think it was five seconds yep and we are returned back to the command prompt and then let's try purchase the book and as we can see it's doing as intended printing out all the books in the data frame now this loop will keep running because we've done a while one loop so to stop it I'm just going to do control C and I'm going to clear my console again so if we go back to the code what you want to do now is basically grab the input of the book ID from the user so finding out what book they want to purchase int input pretty much the same as the menu please enter a number corresponding the options above and also for main menu enter 0 because we also need to allow the user to go back to the main menu if they want to so now the important bit is we check if user enters 0 and just make a comment to remember quit to main menu so add a bit of logic here that does exactly that so if book ID equals 0 so if the user enters 0 there we're just going to break and that's going to lead us back into our main loop which will display the menu to the user again so if the user doesn't then we carry on with the book search so we now actually need to search through our whole data frame or the CSV to find the book that matches the ID the user has entered so book search result will be a variable and then we do data and we use a query option in pandas to basically find the book book the row that matches this book ID so we say book ID double equals and then the book ID variable that the user has given us up here so this will basically grab the row that matches this book ID and if there is no rows it will just return an empty data frame so first off we check if there is actually a book with this ID so we say if book search result the length of the data frame if it's less than one that means there is no book with this ID so just bring invalid option selected please try again and then we'll just do a time dot sleep of let's say five seconds here then we do elif book search result now this elif is basically we're going to be checking next thing we're checking is for availability so if the book was actually found we want to make sure that it's actually available to buy so we do book search result I look zero to basically grab the first row of the result which is going to be the only row anyway and then we do book availability make sure this is this matches your column in the CSB otherwise it will pretty much error so we say elif book search I lock zero book availability is not equal to if it doesn't equal to in stock that means it's out of stock then we print the selected book is out of stock please select a different book and then we print the loading menu in around 10 seconds and then time dot sleep 10 seconds so if the user tries to select the book that isn't available then the user will be notified and then they'll be redirected back to the book menu again where they can see all the books again and select a different book now finally if the book exists within our database and it is in stock this else should cover that so we'll print out the information of the book the user has purchased so you have selected a new line here and a new line at the end and then we'll print all the details of the book title that's gonna be books so I lock zero book title make sure you've got your column names correct here I'm just gonna copy and paste this because I need to do book price so book price and book availability as well so change this to book availability make sure your column names are matching and that's pretty much gonna sort it out now we want to print a bit of like system gimmick so we'll do a tenting to purchase the book with your account balance dot dot dot and we need to time dot sleep here so that the person thinks it's a bit realistic and then we'll print out the users like account balance which we stored in our variable at the start so so your account is pound sign and then plus str balance which is the variable we created at the very start up here so that will print out their balance to them and then now we need to actually check if the user has enough balance to purchase this book so we'll do your balance is greater or equal to book so I lock zero book price so if it is actually equal or greater to the price of the book then you have sufficient funds to purchase the book and then we'll basically have to deduct this sum from the user's balance so balance minus equals books search result I lock zero price now if you don't want to repeat typing this long-ass thing every time you could just save this into a variable but I'm I'm just lazy so I haven't it's probably good practice to save this into a variable actually but since we've already got so far I'm not gonna bother so this line basically deduct the price of the book from the user's balance and then we need to show the updated balance to the user so we'll say your updated account balance is and you can just put a few more lines of printing you have now purchased the book and then we return the user back to the main menu in case they want to purchase now this is in case the user has enough balance so we need to add an else as well here to make sure we cover what happens if the user doesn't have enough balance so we'll say else print insufficient funds to purchase this book and then print the account balance so the user knows why there's insufficient funds and then print once again returning to 10 seconds sleep for 10 seconds our program is pretty much finished so that was a pretty simple implementation of a library system let's just test it and hope for no errors which don't work but let's give it a go anywhere so what we're going to do first try purchasing a book now for the sake of this tutorial what I've done with the first book that shows up is changed it from in stock to like something that isn't in stock so the stock you could change this to out of stock for example which makes more sense so if I were to select book one it should say this book is not available to purchase to select another option or something along those lines so let's try and purchase book number one your selected book is out of stock please try a different book loading menu in 10 seconds perfect it works what if we try to enter a book ID that doesn't exist so let's say 1000 or 2000 actually because the last book here is 1020 2000 says invalid option selected please try again perfect so that's working as well now this time we're actually going to try to purchase a book that we can't afford so I can't balance with like 40 quid so we try to purchase book 1019 it should not allow us to so it says you have selected the book first today which is correct and then the price shows up as well availability in stock it says attempting to purchase the book with your account balance your account balance is 40 and proficient funds to purchase his book and then it shows the account balance again and then it returns us back to the main menu which is perfect this time let's try to purchase a book actually that we can afford so let's do 1020 the book price is 26 pence eight pence so should go through hopefully shows us the details shows us it's in stock which is good and then it shows us the updated account balance now you can change this using the round functionality to basically show you only up to two decimal digits so that's a bit of a challenge for you guys and now if we try to purchase let's say book 1019 which shouldn't let us because updated balance should be 40 minus 26.08 so yeah a bit less than what it was before so it says our account balance is 13 pounds because obviously we purchased the book before and it won't let us purchase this book again so we can now test out the last thing which was press 0 to enter to main menu so press 0 here we go we are back into that main menu from where we can exit or purchase a book so let's exit it's gonna exit in two seconds and that's pretty much it guys I hope you have enjoyed today's tutorial and learned something new if the tutorial was a bit too basic I apologize but this was really designed for like beginners trying to get a nice little program to like brush up their knowledge so I was able to help once again I'd like to thank our sponsors IP Royal for today's video and I really recommend you do check out their proxies because we've got a discounted rate so go ahead and check them out guys and if you have any other video ideas please let me know in the comments below and I'll see your beautiful faces in the next tutorial. Peace.