Python : Expenditure App
Expenditure Application
Add new Entry
Today we will work on the “Add New Entry” function, this is first function on our menu. (Read Expenditure App Menu here)
We will call the function def add_entry(): and will give the user the prompt to enter “Date” and “Amount” then we will write them to our json file.
Just to be as less codes as we can, I am not adding any validations here also not coding the try .. except. So I am assuming the entered data will be in correct way.
def add_entry():
my_date = input(‘Enter the date as dd/mm/yyyy: ‘)
my_amount = input(‘Enter the Amount: ‘)
new_data = {“date”: my_date, “amount”: int(my_amount)}
# Here we adding the new data to the file
ff = open(“expen_dat.json”, “a”)
ff.seek(0, 2) # goto the end of the file.
ff.truncate(ff.tell() – 3) # from end of the file erase last 3 char.
ff.write(‘,\n’)
ff.write(json.dumps(new_data))
ff.write(‘\n]}’)
ff.close()
If you add the “User input choice” in your code this function will be on choice “1” as here.
if choice == ‘1’:add_entry()
choice = the_menu()
if choice == ‘2’:
….
Follow me on Twitter..
Table of posts regarding the Expenditure App..
Title | Description | Post Link |
The Case | Talking about the case study of the application | Click to Read |
leveling the Ground | Talking about: Requirement, folder structure, data file json, sample data and .py file and other assumptions | Click to Read |
The Menu | Talking about: The application menu function | Click to Read |
User input ‘choice’: | Talking about: The user input and the if statement to run the desired function | Click to Read |
Loading and testing the Data | Talking about: Loading the data from json file and print it on the screen. | Click to Read |
Add New Entry | Talking about: The “Add New Entry” function | |
Here is the code shot..![]() |
Here is the output screen..![]() |