Home > Software engineering >  How to get a variable to keep a running total on Python?
How to get a variable to keep a running total on Python?

Time:09-29

I am trying to get this function take_input to add to the total whenever the function is run. However, I am unable to get the variable daily_expense to accumulate when an expense is added a second time.

def take_input():
    daily_expense = 0
    inp = int(input("How much did you spend?: "))
    daily_expense  = inp
    print(f"Total expenses = {daily_expense}")

    while True:
        x = input("Add another expense? Y/N: ").lower()
        if x == "y":
            take_input()
        elif x == "n":
            break
        else:
            print("Please return a valid response")

def start_program():
    start = input("Add expense? Y/N: ").lower()
    if start == "y":
        take_input()

start_program()

CodePudding user response:

You're currently defining daily_expense inside of the function; which means every time take_input() is called, you are resetting daily_expense to 0.

Instead, you should either define daily_expense outside of the function so it will no longer be reset each time; or you should accumulate it all within the loop and then print the final value, like so:

    def take_input():
        daily_expense = 0

        more_expenses = input("Add expense? Y/N: ").lower()

        while(more_expenses == "y"):
            inp = int(input("How much did you spend?: "))
            daily_expense  = inp
            more_expenses = input("Add expense? Y/N: ").lower()

            if(more_expenses == "n"):
                break
            elif(more_expenses == "y"):
                continue
            else:
                print("Please return a valid response")
                more_expenses = input("Add expense? Y/N: ").lower()

        print(f"Total expenses = {daily_expense}")

    def start_program():
        take_input()

    start_program()

Edit: Additional notes:

  • Every time this function is called, the value resets again and reaccumulates.

  • If you want to store this value in a database, then I recommend returning it and then using the returned value to update a database entry like so:

     def take_input():
         daily_expense = 0
    
         more_expenses = input("Add expense? Y/N: ").lower()
    
         while(more_expenses == "y"):
             inp = int(input("How much did you spend?: "))
             daily_expense  = inp
             more_expenses = input("Add expense? Y/N: ").lower()
    
             if(more_expenses == "n"):
                 break
             elif(more_expenses == "y"):
                 continue
             else:
                 print("Please return a valid response")
                 more_expenses = input("Add expense? Y/N: ").lower()
    
         print(f"Total expenses = {daily_expense}")
         return daily_expense
    
     def start_program():
         save_this_number_to_db = take_input()
    
         # store save this number into database
    
     start_program()
    

CodePudding user response:

Use a while loop instead of recursion to continually process user input.

def start_program():
    daily_expense = 0
    extra = ""
    while 1:
        x = input(f"Add {extra}expense? Y/N: ").lower()
        if x == "y":
            inp = int(input("How much did you spend?: "))
            daily_expense  = inp
            print(f"Total expenses = {daily_expense}")
            extra = "another "
        elif x == "n":
            break
        else:
            print("Please return a valid response")

CodePudding user response:

This is the working code

def take_input(daily_expense:int):
   
    inp = int(input("How much did you spend?: "))
    daily_expense  = inp
    print(f"Total expenses = {daily_expense}")

    while True:
        x = input("Add another expense? Y/N: ").lower()
        if x == "y":
            take_input(daily_expense)
        elif x == "n":
            break
        else:
            print("Please return a valid response")

def start_program():
    daily_expense = 0
    start = input("Add expense? Y/N: ").lower()
    if start == "y":
        take_input(daily_expense)

start_program()
  • Related