This Python program is an implementation to CS50’s Introduction to Programming with Python Week 8 - Cookie Jar Problem Set. The Cookie Jar program, implemented in jar.py
, simulates a cookie jar with the following methods:
__init__(self, capacity=12)
: Initializes a cookie jar with the specified capacity. Raises a ValueError if the capacity is not a non-negative integer.__str__(self)
: Returns a string representation of the cookie jar using 🍪 characters to indicate the number of cookies in the jar.deposit(self, n)
: Addsn
cookies to the jar. Raises a ValueError if the deposit would exceed the jar's capacity.withdraw(self, n)
: Removesn
cookies from the jar. Raises a ValueError if there aren't enough cookies in the jar.capacity(self)
: Returns the jar's capacity.size(self)
: Returns the number of cookies currently in the jar.
-
Open your terminal.
-
Navigate to the directory where you have saved the
jar.py
file.cd path/to/your/directory
-
Run the program using the
python
command:python jar.py
-
The program will interactively prompt you to test various functionalities of the cookie jar.
The Jar
class effectively simulates the behavior of a cookie jar. Here's how it works:
- The
__init__
method initializes the cookie jar with a specified capacity. - The
__str__
method returns a string representation of the cookie jar using 🍪 characters. - The
deposit
method adds cookies to the jar, adjusting the size attribute accordingly. - The
withdraw
method removes cookies from the jar, updating the size attribute. - The
capacity
property returns the capacity of the cookie jar. - The
size
property returns the number of cookies currently in the jar.
-
Open your terminal.
-
Navigate to the directory where you have saved the
test_jar.py
file.cd path/to/your/directory
-
Run the tests using the
pytest
command:pytest test_jar.py
-
The tests will run, and you will see the results indicating whether the program passes each test case.
- The program uses a class structure to create a virtual cookie jar with various functionalities.
- The tests provided in the
test_jar.py
file ensure that the program behaves as expected. - The class methods and properties are designed to handle various scenarios, including invalid inputs and exceeding the jar's capacity.
- Remember to thoroughly test the program and ensure it meets all requirements.