Why don't the pop() and peek() methods have return statements for the empty stack condition?


#1

Question

In this exercise, the pop() and peek() methods do not have return statements for the case where the stack is empty. What is the behavior for the methods when the stack is empty?

Answer

Python functions will default to returning the value None if there is no explicit return. For this exercise, the pop() and peek() functions will print a message indicating that there is an error condition and then the methods will return with the default value. The calling code for the methods can check to see if None is returned.