5 Replies - 2295 Views - Last Post: 15 August 2012 - 05:33 PM Rate Topic: -----

#1 mike316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-July 12

error in my running total in my while loop.

Posted 15 August 2012 - 05:00 PM

I was trying to do a budget analysis with python. I want it to calculate a running total on my expenses. For example when I type in a expense of $200.00 and the program asks if I want to stop the program and I type 'no' it asks for my expense again, so I type in $ 300.00 I then type yes to end to program, it will then ask how much I budgeted for the month I type in $600.00. Instead of calulating 200 + 300, it will only calculate the last one $300 for a total of $300 dollars. Can someone please help me with what I've done wrong. Thanks in advance.

declaring the variables

def main():
    endProgram = "No"
    while endProgram =="No":
        expense = 0
        total = 0  
        expense = get_expense(expense)
        endProgram = raw_input('Did you want to stop? (End program Yes or No)')
    totalBudgeted = get_budget()
    calcBudget = calc(total,totalBudgeted,expense)
    printInfo(calcBudget)

#The functions below. This function asks you how much you want to
# to budget for the month, for example you want to budget $4,000.00

# The function gets the amount of expenses for the month.

def get_expense(expense):
    expense += input('Enter your expense here:')
    return expense


def get_budget():
    amountBudgeted = input('How much do you want to budget?(0 if none)')
    return amountBudgeted


# The function below figures out the
# balance.
def calc(total,totalBudgeted,expense):
    calcBudget = total + totalBudgeted - expense 
    return calcBudget

# The function below prints the results.

def printInfo(calcBudget):
    print 'The total budget for the month if negative you are under budget: $%.2f' % calcBudget
main()



I forgot I'm really new with this python stuff, so could we try to keep it as simple as possible there are so many brillant people here, and most are head and shoulders above me. Thanks again in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: error in my running total in my while loop.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,182
  • Joined: 21-June 11

Re: error in my running total in my while loop.

Posted 15 August 2012 - 05:07 PM

Let's walk through what happens:

1. The program starts and main() is called.
2. The while-loop starts.
3. On line 6 expense is set to 0.
4. get_expense is called with 0 as its argument.
5. The user enters an expense. The new value of expense is what the user entered.
6. The user is asked whether to continue and he enters "Yes", so the while-loop repeats.
7. It goes back to line 6 where expense is set to 0.
...
See the problem?
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5438
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: error in my running total in my while loop.

Posted 15 August 2012 - 05:09 PM

I don't program in Python but...

Line 5 starts your while loop.
Line 6 you set expense to 0. You do this every iteration through your while loop. So it doesn't really matter if you add to it online 20 or not: It was 0 when you add the user input.

You never seem to add anything to Total, but again, I might be missing something since I am outside of my normal language.

So where (what variable) do you think is the running total of expenses that you keep adding the newly enter expense to?

You probably what to move that initialization of exp
Was This Post Helpful? 0
  • +
  • -

#4 mike316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-July 12

Re: error in my running total in my while loop.

Posted 15 August 2012 - 05:15 PM

View Postsepp2k, on 15 August 2012 - 05:07 PM, said:

Let's walk through what happens:

1. The program starts and main() is called.
2. The while-loop starts.
3. On line 6 expense is set to 0.
4. get_expense is called with 0 as its argument.
5. The user enters an expense. The new value of expense is what the user entered.
6. The user is asked whether to continue and he enters "Yes", so the while-loop repeats.
7. It goes back to line 6 where expense is set to 0.
...
See the problem?


Yes, but don't I need a variable to accumulate the expenses?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,182
  • Joined: 21-June 11

Re: error in my running total in my while loop.

Posted 15 August 2012 - 05:19 PM

You do, but you don't have to set it to 0 every time the loop repeats. You can set it to 0 once before the loop starts.
Was This Post Helpful? 0
  • +
  • -

#6 mike316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-July 12

Re: error in my running total in my while loop.

Posted 15 August 2012 - 05:33 PM

View Postsepp2k, on 15 August 2012 - 05:19 PM, said:

You do, but you don't have to set it to 0 every time the loop repeats. You can set it to 0 once before the loop starts.


Thank you Sepp, I got it to work no. Much appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1