Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Generate a Simple Expense Report | Visualizing and Presenting Information
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Daily Tasks

bookChallenge: Generate a Simple Expense Report

Tarefa

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.

Solução

12345678910111213141516171819
def 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)
copy
123456789101112
def 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)
copy

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.
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain how the columns should be aligned if the item names are longer?

What should the function do if the expenses list is empty?

Can you show an example with different data?

close

bookChallenge: Generate a Simple Expense Report

Deslize para mostrar o menu

Tarefa

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.

Solução

12345678910111213141516171819
def 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)
copy
123456789101112
def 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)
copy

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.
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5
single

single

some-alt