LearnFrenzy
Sign in Take a free test

Lists · Find the Error · Question 1

Error: pop() from an empty list Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

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

main.py
undo_stack = ["type A"]
undo_stack.pop()
undo_stack.pop()
print("Undone twice")
  • A ValueError: "type A" not in list
  • B It prints Undone twice
  • C TypeError: pop() needs an index
  • D IndexError: the second pop() runs on an empty list; check if undo_stack: first Correct
Correct Answer: Option D

pop() needs at least one item. Guard it with if undo_stack:.

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