Home > OS >  Python (Sum of expense, income, and profit)
Python (Sum of expense, income, and profit)

Time:06-27

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)

CodePudding user response:

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)

CodePudding user response:

your profit in for loop will be reset equal to 0 when you decide transaction is expense or income

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
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)

I think it will be solve your problem

CodePudding user response:

You have an issue in all of your loops that you are setting the accumulator (income, expense, profit) to 0 inside the loop, instead of before it, so you are losing any prior transaction data. Also you are printing income and expense totals in the loop, rather than after the loop. Finally, you can simplify your code by using comprehensions and the builtin sum function:

entries = entries = [
    {'date': '2021-01-01', 'transaction': 'Expense', 'amount': '50', 'note': 'Lemons'},
    {'date': '2021-01-02', 'transaction': 'Income', 'amount': '100', 'note': 'Sales'}
]

income = sum(int(e['amount']) for e in entries if e['transaction'] == 'Income')
expense = sum(int(e['amount']) for e in entries if e['transaction'] == 'Expense')
profit = income - expense
print(f'Income\t${income}', f'Expense\t${expense}', f'Profit\t${profit}', sep='\n')

Note that you don't need to iterate to compute profit, you can simply subtract expense from income.

  • Related