Create Your Own Expense Tracker Using Python | Step-by-Step Tutorial

Akansha Yadav builds a command-line expense tracker in Python using a dictionary to store categories and amounts. The app uses a menu loop with functions to add expenses, view totals by category, and update entries, plus input validation and error handling.
Chapters
Okay, so just like I'm writing on my screen, you have— we have to create a command line application to track and manage the expenses by category. Okay, because we are not going to add any GUI, we haven't learned about any of the libraries yet, so we cannot add GUI. So this is going to be all command line based. Okay, so where we can track, manage by categories.
Okay, so we can use dictionaries also in this one.
Okay, so let's start creating this. Okay, so first thing that we need is one— we first thing we need is the empty dictionary. Why do we need empty dictionary?
To store values.
Correct. Basically, we are going to create one dictionary, and why we have selected dictionary is Because in this I wanted to make it category-wise, so I want category to be my keys and the amount that I have spent to be the value. So that is the reason I am selecting the dictionary as a data type to store my values. Any question in this? Let's say I'm writing expenses is equal to curly bracket We are just initializing an empty dictionary so that later on whenever we wanted to add something in it, like related to adding expenses or adding some values in it or adding categories in it, I can do it later on. But for now I wanted to make it just empty. Just give me a minute.
Ma'am, can you repeat?
Yeah, am I audible to you now?
Yes, ma'am, can you repeat again?
Yeah, I'm saying that we have selected a dictionary. Why? What, what do you think why we are selecting dictionary?
Because it can easily manage key and value, like, uh, category and that Right, and we are—
why we are making it empty?
Because we can use it as a user input, like whatever expenses are there, we can easily add on our own, the user end.
Correct. So first thing is we have to create a dictionary for this. Now next thing is I want some details to be presented in front of the user. So let's say I'm creating one function which is going to be my display function. Let's say display menu. This is just a simple tracker, so I'm going to display a menu where it will be. I'm going to use just the print statement. So this is my expense tracker, expense tracker menu. Next is going to be Okay, so then I'm going to display what are the features of this. So first is I can— I make— I wanted to make the user to like that user can add expenses. Second thing that I want from the user is that that user can view the total expense. Total expense means overall how much you have spent in that particular month, let's say. And third thing is going to be, if I want— if the user wants to view it category-wise, then that feature also I want to give. So third is going to be view category-wise expenses. Okay, so next thing which we have decided is, yeah, that user can update it, update expenses. And now let's say user don't want to use it, so user wants to exit from the expense tracker. Now the first thing is add expense. So whenever it comes to add expense, obviously if user is going to— like user wants to add expense, then we need a function for add expense as well. I'm making it function by function because anytime if user wants to reuse it We don't have to write down the same code again. So let's say add expenses. Now while user is going to add it, so I wanted to first mention the category, that which category, and then the second thing is amount, that how much you have spent for this particular thing. If let's say category— okay, let's take an example, like whenever user is adding, let's say for this month on food I have spent 2,000 bucks. Now it's the January, and I'm again going to add my details in my expense tracker saying that this month I have spent $2,500. So last month when I made a new entry, I have to add that this is for food. But this month when I'm trying to add, then I don't need to mention the category. I just have to enter the value, which is the new expense. So in that case, I don't need to enter category. So what I'm going to do is, while, while my program will ask me to enter the category, but I'm not going to provide. If I will provide the category, then you— then my program will basically check it, that category exists in the expenses variable, which is a dictionary, or not. If it exists, then within that expense variable itself, within that category, uh, key, I want to add up that amount, like this month I spent 2,000 and next month I'm spending 2,500, then it should be added. So I'm going to add that amount. Is this clear to everyone? Yeah, is this clear to everyone?
Yeah. Uh, ma'am, uh, this category you have written expense category, uh, so we are targeting what exactly? What does this say exactly?
Which one?
The third line in add expense. Yes, ma'am.
So this is basically our dictionary, right? From dictionary, this is my key category.
Yeah.
I'm taking that value from the category. Okay, if it exists, if it exists, like it means if this condition is going to be true, then only this statement will be printed, right? So this statement says that from the dictionary, from in the category key, you have to go and update the amount with the addition. So basically, if the existing value is 2000, then you have to say 2000 2500, and it will add it as 4500. So it will go as food 4500. Previously it would be like, let's say, food 2000. So now it will be updated to food 4500. Did you get it now?
Yes, ma'am.
So we are basically updating the value. This is an expense tracker for an year, let's say. So for year, like, there are 12 months. So for month by month, I'm adding up the value. So whenever I'm adding up the values, I need not to enter the category again and again. If that category exists, I just need to update the amount because up till now, in just, in let's say 2 months of this year, I have spent 2000 or 2500, whatever the amount is, I wanted to add it up. So when it comes to add it up, you can just use this way to do it. Okay, now next thing is when the user wants to view it, when you want to view the total, then simply we have to— we are creating a new function which is going to be view total expense. And within this, I don't need any variable or any parameters, so I can just use one within it. Total is equal to sum of— now in this, I'm using the sum inbuilt function, and I'm going to apply it on the values of the expense tracker. All the values will be added directly. And then I'm going to use one print statement. Let's say total expense. Okay, this for us, you can display it in dollars or you can display it even in rupees. Okay, now next is I wanted to view—
sorry, return function we are not using, right?
Because we didn't— we don't want to make any further calculation on this. Now next is if user wants to go with the category-wise view, so view category expense. When it comes to category expense, then simply we say like, I'm going to display— or else let's just directly go with the for loop. For categories. So in this case, I am going to use two local variables for for loop because I will show you like— first let me just complete this statement to make it more clear to you. So when it is expense.items, you remember that I told you that expense.item function. So this expense.item is going to have two values. First will be the key, second will be the value. So when it comes to the pair, like this is going to generate two values. So obviously we need two values, two variables to store it for a short period of time. So I'm creating two variable category and amount. Then I am going to display it. Print, let's say, category, and let's say amount. Okay, or let's say, let's category and amount directly I'm writing. So I'm just going to add one more print statement to make it more clear. But this is category-wise expense. Okay, so next is updating an expense. So now let's create one function to update it. So def update_expense Now which are variables? So we wanted to update an expense, so this is going to be again with the category and amount. But this time I'm going to make the change in the amount variable because we have already used amount, so we will use new amount variable. Now with the new amount, first is I have to— obviously I have to check whether that particular category exists in the expenses or not. If it exists, then only I'm going to update my expense this dictionary category key with the new amount. And I'm going to display it again. Let's say category updated to, let's say, new amount. Whatever user wants to add it up. Else I wanted to display, let's say, that category doesn't exist. So I can simply say that category doesn't exist, or category not found. Simple. Any question in this? Yeah, anyone, any question in this? No questions. Okay, so up to now we have created 4 different functions for all the features that we have added. Now next is exit. When it comes to exit, then I am going to make it— and like, I'm going to make it within a loop which is going to an infinite loop. So whenever we create a simple applications like command line application, or we are creating one text-based program, right? So you have to, uh, when we are using the functions and all, you can simply write it within the while loop directly. But okay, let's go with the while loop first, then I will explain you that concept, then it will be more clear. So simply I'm writing while True. I have already told you that when you are going to write this true/false variables, uh, true/false Boolean values in while loop, then it is going to make it infinite loop or will help you get the output again and again and will make it work again and again until and unless user is going to make any user attempted— like you, a user wants to exit it. Okay, so when you're writing while True, it means it will continue until user is going to say exit. You can see our 5th option is exit. So I want that whenever user is going to choose this 5, then only this program should terminate, otherwise this should go on. Now within this, first thing what I want— I have created all the functions, obviously without calling those functions, those functions are not going to work. So we have to call those functions as well. So first thing that I wanted to display is the menu. So I'm going to call my function which is display_menu. So simply I'm calling my function. That function itself consists of multiple print statements, so all the print statements will be printed at once. Now next thing is I want to change— I want the user to make a choice, so I need a choice variable is equal to input brackets enter your choice. It's going to be, let's say, 1 to 5 because we have 1 to 5 values in it. Okay, now if the choice of the user is equal to 1, then what do you want? I have to use double equal to it. Yeah, now what do we want? We want that in, in the, in the first one, which if you remember, add expenses one, we have two variables. First is category, second is amount. So for category, we need the input from the user. So now we are going to take that input. So we are going to say, let's say category, same variable, is equal to input bracket enter category. Okay, next thing is amount. Amount is equal to— let's say I am making it flexible with the float. Now I'm going to take input brackets, enter amount. Now I have to check if amount is greater than 0. Then I'm going to add— I'm going to call that function directly, addExpense function, and I'm going to pass both the values which I have just taken from the user. Okay, and then I will print simply one statement that amount has been added. You can write this statement, this print statement, there itself in the function also, but I'm writing it over here because we haven't written it above. So added, you can say amount to category. Okay, or else I want to display that zero shouldn't be added because that makes no sense in the expense tracker. So I'm going to display That amount must be positive. Any question in this? Yeah, anyone, any question in this? Lower. Okay, so when we are create this with the multiple options, same thing we are going to do with the other functions as well. But in case of other functions, we don't need to take multiple values because we have already taken the values. So for 2 and 3, we are going to make it just with the elif. So let's just remove This elif choice is equal to equal to, let's say, 2. Our second function is this view total— view total expense. So we are going to say view total expense. Then again elif choice is equal to equal to 3. Then the third one will be viewCategoryWiseExpense. So obviously the data is already there, so we need not to take any input or anything. So we directly— we are calling the function. Then choice is equal to equal to 4. In case of 4, what do we need? We need 2 values again, right? First is going to be the value of category, and the second thing is going to be the amount. Right, yes, category and new amount. Yeah, so same code I'm taking from here, okay, and I will paste it over here. Now this time I'm going to make it new amount and new amount I'm going to add again. Greater than or equal to we should use. We shouldn't use just greater than. Okay, anything else which you feel we should change? Amount greater than or equal to 0, right? Yeah, correct, correct.
That add expense also we have to change, or it will be the same?
No, no, no, no, no. For, for fourth, we have already created a different function, so we will use that function, which is this one. And again, the new amount should be— yeah. Anything else? I think that's it.
We don't need this else, right, in this, or we need it?
We need it because again we are taking the input of new amount, that if again that is 0, right? So the previous value itself will be deleted, and that too will make no sense in the expense tracker. Okay, okay. So simple. I think we have updated everything.
So ma'am, what if there is no category in while updating? If we don't find any category, then what it will say?
Which category?
Like, uh, suppose there is a food, F-O-O-D. I wrote F-U-D. So will it show something error or what exactly it will do, or it will create a new category?
No, no, no, it's basically when we are creating a category in program first one. Yeah, this function itself is going to check that category, right? Okay, whether that category exists or not. If it exists, then only it will add it, otherwise it will say that category not found directly. Okay, so that is why we are not mentioning it in the main function. Okay, okay, next is choice is equal to equal to 5. In case of 5, what do we want? We want this entire loop to stop, correct? So what do you think, which statement should we use in this case?
Break.
Break, correct. So now we have to use break statement. So I'm going to simply say, let's say, goodbye, and okay, and I will just add a break statement. Over here, correct? Okay, okay, we'll just run it and we'll see if there is any error or something. Okay, so it is displaying the menu to us. Now it's asking us the choice. So let's say I wanted to make a new entry, which is I'm going to add an expense. So when it comes to adding expense, it's asking for the entry. So let's say I'm writing Food. Enter. Now the amount, let's say 1,000. Enter your choice. I wanted to again make an entry, so again I'm using 1. Now again I have to choose, so let's say I'm writing travel, and let's say 2,000. Again it's asking me to make a choice that what do you exactly wanted to do. Again I wanted to make an entry. Now let's say rent, I'm going to say, let's say 5,000. Okay, now I wanted to update that 5,000 to 4,000. Okay, so I'm going to choose 4. Now which one I was supposed to update? I was supposed to update the rent one. So I'm going to write down rent. When I've written rent, it's asking me for the amount, so I'm going to add 4,000. Now I will just try to view it. So I'm going to make an entry 2, and I'm going to exit. Wait, I think there was something wrong.
We were not able to see view function.
Yeah, I think view wasn't working. So let me just rerun this. So where is the output? Yeah, with the view function, we are not able to get that view function, right? Let me just try with the tree whether we are able to Yeah, category-wise expense we are getting. Food is 1000. I have just made one entry. Oh, we are getting the total expense now over here. This is the total expense. Let me add more entries. Okay, so let's say Actually, my output is getting— I'm not able to view the complete output, so I will just exit for now and I will copy the entire program and we'll keep it in the new file, expense tracker.
5.
I will add it up and we'll execute it. Now it will be more flexible because this is command line one, that is why. So food, let's say 1,000. Now I wanted to view, so I'm going to say view total. It's saying total expense 1,000, which is correct. Now I wanted to again make an entry. So let's say I'm adding rent for 4,000. Again, one, let's say travel, which is 1,500. And now I want to view the total. So total is 2,900, correct?
Yes, ma'am.
Now I wanted to view the category-wise one. So this is my category-wise. For food, I have spent 1,000. For rent, 400. I think I missed one zero. Travel, 1,500. Okay, now I'm going to update rent from 4,400 to 4,000. Okay, so I'm going to use 4, category, let's say rent, and it should be 4,000. Enter. Added 4,000 to rent. Now I'm going to view it category-wise. So 4000 has been updated. Any question in this?
Uh, no, ma'am.
Now let's see, let's make some modification in the program. Now, now we are going to upgrade it basically, okay? So let's say I'm going to say 4. Last time I entered rent in lowercase, now I'm going to make it in the uppercase. Now let's test it. Added 1,000 to rent. Now I'm going to make it category-wise view. You can see rent not found, but why it's displaying this? Let me just check that function again. This is the one, right? If this is the case, it will add it. No, no, no, not this one. This is not required. Okay, now I will just run this program again. Food, 1000. Total expense, 1000 only. First, let's say Travel, 2000. Total is 3000. Now, now I'm going to view it category-wise. 4000 Travel. Fourth, I'm going to use which is update function, and now this time I'm going to use Travel in uppercase, and amount that I'm going to add is, let's say It says travel not found. Now it's working perfectly. Travel not found. When, when we are trying to use something which is already in the function, in the category, but we are not getting, which function do we, we should use in the string that we are adding to make it happen? Okay, just a simple question with— to everyone. Okay, now we have written a complete program. To make minor changes like related to text or something like being very active, like which functions related to string should we add it up in this program? No, I wanted to make it more specific, okay, and make it more user-friendly, okay? So which functions, which inbuilt function of string concept should we apply in this program? Okay, so first thing which I think is like whenever user is going to make an entry for the category, right? So whenever user is going to enter categories, then obviously maybe user might enter some extra whitespaces, maybe just tabs or something. Okay. But next time when user is trying to search it, it will be basically like not that much convenient for the user. Like last time mistakenly, like that person has added maybe, but now the user is making the current entry, then at that case, in that case, like user will feel that, okay, this is not worth it, like this is not working the way it should, right? So which function should we use? Sorry.
Lord, not Lord.
I didn't get you.
We can use this one, like in string we can use lowercase or uppercase so that whatever user will enter, it will be in a proper form.
Correct. So we have to make it specific, like if you want everything, like your program is dealing within lowercase So you make all your entries in the lowercase just by writing .lower. Now where do you have to write that function? So whenever you are taking the entry, there itself, like suppose this is the entry which you are taking, so write .lower. Automatically that will be converted into lowercase and will be stored in the same variable. Any other function? We can also use strip function, right? .strip, which will help us remove extra spaces entered by the— and like, this is the case of, uh, like float value, so here it is not required, but we can add it somewhere over here. So these kind of functions can make it more accurate. Right, you also have to think about some errors that this program or that values can generate, basically. Okay, so you have to think about, like, let's say there is a case that it will generate value error sometime. Okay, like related to these 1, 2, 3, 4. Okay, so if I'm going to just run this program and we'll say 6, is it doing something? No. If I'm saying 7, are we getting something in the output? 8, 9— no, because there is no restriction for that. We have just mentioned up till 5. If user will enter 5, then it will break. But what about others number? Like if the user is going to enter 6, 7, 9, 10, then what?
We have to show like incorrect choice.
Correct. So for those thing also you have to, uh, maintain like your code should cater those as well. Like it is related to Value error, if it is related to invalid values, then those errors should be displayed in the user-friendly way, just like we are displaying these messages, like these things like add expense and all. Like, if we are going to display the errors also in this manner, making it more meaningful to the user, then user will be able to correct it quickly and it will be more user-friendly. Okay, so when it comes to development, so you have to take care about all these parameters. Got it? Now anyone, any question in this?
In interview, what kind of questions they ask? Like, like this kind of questions they ask?
Yeah, they, they can ask you related to the errors. Like if, if you are going for the development role, right? It depends from role to role as well. Like if you're going for the analysis role, they're not going to ask you these kind of questions, okay? They will ask you the questions related to the libraries like Pandas, NumPy, or they might ask you which type of graphs you are creating and all. But if you are going for the developer role, they will ask you the questions that the errors are, okay? So to just to check like how good you are in debugging or how good you are in developing, okay?










