With stacks, size matters. If we’re not careful, we can accidentally over-fill them with data. Since we don’t want any stack overflow, we need to go back and make a few modifications to our methods that help us track and limit the stack size so we can keep our stacks healthy.

What do we do if someone tries to peek() or pop() when our stack is empty?

How do we keep someone from push()ing to a stack that has already reached its limit?

How do we even know how large our stack has gotten?



In __init__(), let’s add two new properties: size and limit.

limit should be accepted as a parameter with a default of 1000. Inside the method, set the instance limit property to the passed in value of limit.

size should be set to 0 in __init__().


In peek() and pop(), wrap the current body of each method in an if clause that checks if the size of the stack is greater than 0.

At the end of each method, outside the if clause, add an else clause with a print statement to let users know that the stack is empty.


In pop() just before the return statement, reduce the size of the stack by 1.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?