0

This is my main function and another function it keeps having a problem with

def main():
    """
    Main algorithm to calculate monthly budget
    """

    # To run again variable
    run_again = 'Y'

    # Expense variables, 0 to quit
    while run_again == 'Y':
        total_budget = budget()
        expense_val = expense()
        while expense_val != 0:
            total_budget = total_budget - expense_val
            expense_val = expense()

        print('The leftover budget is $', '%0.2f' % total_budget, sep='')

        run_again = get_run_again()


def get_run_again():
    """
    Asks the user to validate Yes and No

    arguments:
    None

    Returns:
    Yes and No
    """
    # Ask to enter Y for "yes, run again" or N for "no, quit"
    get_run_again = str(input('Y for yes,or N for no: '))

    # Ask user to user to validate yes or no
    while get_run_again != 'Y' and get_run_again != 'N':
        print('Run again?')
        get_run_again = str(input())

    return get_run_again
shahaf
  • 4,750
  • 2
  • 29
  • 32

1 Answers1

1

This is because you have a local name get_run_again in your get_run_again function which takes precedence and hides the global name that is the name of the function itself. This confuses the interpreter.

If you change the name of the variable to be different from the function name; this error will disappear. [Or change the name of the function]

Another [not a good] way would be to declare the get_run_again variable within the function as global using the global keyword.

Nidhin Bose J.
  • 1,092
  • 15
  • 28