LearnFrenzy
Sign in Take a free test

Dictionaries · Find the Error · Question 5

Error: sum() over a dictionary Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

The program below stops with an error. Which statement correctly describes the problem?

main.py
cart = {"book": 450, "pen": 20}
print("Total:", sum(cart))
  • A TypeError: iterating a dict gives its keys (strings), so sum() tries 0 + "book"; use sum(cart.values()) Correct
  • B It prints Total: 470
  • C KeyError: book
  • D ValueError
Correct Answer: Option A

Anything that iterates a dict sees the keys unless you ask for values() or items().

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links