Challenge: Generate a Simple Expense Report
Tarea
Swipe to start coding
Write a function that prints a neatly formatted table of items and expenses, including the total expense at the end.
- Print each item and its amount, formatted in aligned columns.
- After listing all items, print a row with the total amount, labeled "Total".
- The amount for each item and the total should be displayed with two decimal places.
Solución
12345678910111213141516171819def print_expense_report(expenses): print("Item | Amount") print("-------------|-------") total = 0 for item, amount in expenses: line = f"{item:<12} | ${amount:>6.2f}" print(line) total += amount total_line = f"{'Total':<12} | ${total:>6.2f}" print("-------------|-------") print(total_line) expenses = [ ("Groceries", 54.32), ("Utilities", 120.00), ("Coffee", 8.50), ("Internet", 45.99) ] print_expense_report(expenses)
123456789101112def print_expense_report(expenses): print("Item | Amount") print("-------------|-------") # Write your code here expenses = [ ("Groceries", 54.32), ("Utilities", 120.00), ("Coffee", 8.50), ("Internet", 45.99) ] print_expense_report(expenses)
Write a function that prints a neatly formatted table of items and expenses, including the total expense at the end.
- Print each item and its amount, formatted in aligned columns;
- After listing all items, print a row with the total amount, labeled "Total";
- The amount for each item and the total should be displayed with two decimal places.
Unit Test Checklist
- Each item and its amount are printed in aligned columns;
- The total amount is printed after all items, labeled "Total";
- Amounts, including the total, are formatted with two decimal places;
- The function handles an empty list of expenses by showing only headers and the total as 0.00.
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 3. Capítulo 5
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Genial!
Completion tasa mejorada a 5.56
Challenge: Generate a Simple Expense Report
Desliza para mostrar el menú
Tarea
Swipe to start coding
Write a function that prints a neatly formatted table of items and expenses, including the total expense at the end.
- Print each item and its amount, formatted in aligned columns.
- After listing all items, print a row with the total amount, labeled "Total".
- The amount for each item and the total should be displayed with two decimal places.
Solución
12345678910111213141516171819def print_expense_report(expenses): print("Item | Amount") print("-------------|-------") total = 0 for item, amount in expenses: line = f"{item:<12} | ${amount:>6.2f}" print(line) total += amount total_line = f"{'Total':<12} | ${total:>6.2f}" print("-------------|-------") print(total_line) expenses = [ ("Groceries", 54.32), ("Utilities", 120.00), ("Coffee", 8.50), ("Internet", 45.99) ] print_expense_report(expenses)
123456789101112def print_expense_report(expenses): print("Item | Amount") print("-------------|-------") # Write your code here expenses = [ ("Groceries", 54.32), ("Utilities", 120.00), ("Coffee", 8.50), ("Internet", 45.99) ] print_expense_report(expenses)
Write a function that prints a neatly formatted table of items and expenses, including the total expense at the end.
- Print each item and its amount, formatted in aligned columns;
- After listing all items, print a row with the total amount, labeled "Total";
- The amount for each item and the total should be displayed with two decimal places.
Unit Test Checklist
- Each item and its amount are printed in aligned columns;
- The total amount is printed after all items, labeled "Total";
- Amounts, including the total, are formatted with two decimal places;
- The function handles an empty list of expenses by showing only headers and the total as 0.00.
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 3. Capítulo 5
single