[Python] Why doesn't my while loop stop when I enter "done" in my Python input?

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I'm building a budget tracker in Python where the user can input expenses in a loop. I want the loop to stop when the user types "done". But , even when I type "done", the loop continues asking for input.

    Here's my function:

    def get_expenses():
    expenses = []
    amounts = []

    while True:
    expense = input("Enter the name of your expense or 'done' to finish: ")
    if expense.lower() == "done":
    break
    amount = float(input(f"Enter the amount for {expense}: $"))
    expenses.append(expense)
    amounts.append(amount)

    return expenses, amounts


    And I'm calling it like this in main() function

    expenses, amounts = get_expenses()

    But the loop doesn't break when I type "done". What could be causing this? I'm running this in VS Code on Python 3.11.

    Continue reading...

Compartilhe esta Página