In Python I have a list of entries. I need to get the sum of expenses, the sum of income, and the profit. I've been able to get the expenses and income. Looking to break out the profit. I know I'm missing something simple but can't figure it out.
entries = entries = [
{'date': '2021-01-01', 'transaction': 'Expense', 'amount': '50', 'note': 'Lemons'},
{'date': '2021-01-02', 'transaction': 'Income', 'amount': '100', 'note': 'Sales'}
]
#Print Income
for entry in entries:
if entry['transaction'].lower() == 'income':
income = 0
income += int(entry['amount'])
print(f"The total income is ${income}")
# Print Expense
for entry in entries:
if entry['transaction'].lower() == 'expense':
expense = 0
expense += int(entry['amount'])
print(f"The total expenses are ${expense}")
# Print Profit
for entry in entries:
profit = 0
if entry['transaction'].lower() == 'expense':
profit -= int(entry['amount'])
elif entry['transaction'].lower() == 'income':
profit += int(entry['amount'])
print(profit)
Looks like you just want to calculate the profit/expenses/income for the entire list, so you shouldn't set "profit = 0" after every iteration, instead just set it in the beginning.
entries = entries = [
{'date': '2021-01-01', 'transaction': 'Expense', 'amount': '50', 'note': 'Lemons'},
{'date': '2021-01-02', 'transaction': 'Income', 'amount': '100', 'note': 'Sales'}
]
#Print Income
income = 0
for entry in entries:
if entry['transaction'].lower() == 'income':
income += int(entry['amount'])
print(f"The total income is ${income}")
# Print Expense
expense = 0
for entry in entries:
if entry['transaction'].lower() == 'expense':
expense += int(entry['amount'])
print(f"The total expenses are ${expense}")
# Print Profit
profit = 0
for entry in entries:
if entry['transaction'].lower() == 'expense':
profit -= int(entry['amount'])
elif entry['transaction'].lower() == 'income':
profit += int(entry['amount'])
print(profit)