Python : Expenditure App
Expenditure Application
Loading and testing the Data:
In this post we will load the data from json file and print it to the screen, also will use one of our functions called “get_year_total” to get the sum amount in a given year.
First we assume the our file.json is there in the same directory of the Expenditure.py file (read previous post here) so our code will upload the data in a variable named “data” and we will print the file to our screen, this line of code (print to screen) is there in our coding process and should be removed from the last version of our application.
# This function will load the json file data into var data and return it back so we can use it
def get_all():
with open(‘expen_data.json’) as f:
data = json.load(f)
return data
# This function will take a year from the user input and return the total of amount we expend in that year.
def get_year_total(my_year):
tot=0
for my_date in data[‘expenditure’]:
if my_year in my_date[‘date’]:
tot=tot+int(my_date[‘amount’])
return tot
data = get_all() # Load the file in to data
print(data)
# Ask the user to Enter a year.
my_year=input(‘Enter a year as yyyy: ‘)
# pass the year to the get_year_total function
total=get_year_total(my_year)
# print the total
print(total)
If every thing goes fine, you should see the data on your screen and a prompt will ask you to enter a year, at this point we are not doing any try … except block to our code so we assume that we are entering a good/right inputs. I am attaching images for the code and output screens.
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 |
Here is the code shot..![]() |
Here is the output screen..![]() |
-
December 4, 2018 at 10:52 amPython : Expenditure App | Ali's Photography Space...