Cookie Jar

Cookie Monster

Source: Sesame Street

假设 you’d like to 实现一个 cookie jar in which to store cookies. In 名为 jar.py, 实现一个 class called Jar with these methods:

  • __init__ should initialize a cookie jar with the given capacity, which represents the maximum number of cookies that can fit in the cookie jar. If capacity is not a non-negative int, though, __init__ should instead raise a ValueError (via raise ValueError).
  • __str__ should return a str with n 🍪, where n is the number of cookies in the cookie jar. For instance, if there are 3 cookies in the cookie jar, then str should return "🍪🍪🍪"
  • deposit should add n cookies to the cookie jar. If adding that many would exceed the cookie jar’s capacity, though, deposit should instead raise a ValueError.
  • withdraw should remove n cookies from the cookie jar. Nom nom nom. If there aren’t that many cookies in the cookie jar, though, withdraw should instead raise a ValueError.
  • capacity should return the cookie jar’s capacity.
  • size should return the number of cookies actually in the cookie jar.

Structure your class per the below. You可能不 alter these methods’ parameters, but you 五月 add your own methods.

class Jar:
    def __init__(self, capacity=12):
        ...

    def __str__(self):
        ...

    def deposit(self, n):
        ...

    def withdraw(self, n):
        ...

    @property
    def capacity(self):
        ...

    @property
    def size(self):
        ...

演示

You’re 欢迎, but not 必需, to 实现一个 main function in which to test your class, so this is all we can demo!

Cookie Monster

Source: Sesame Street

Before You Begin

  1. Log into cs50.dev using your GitHub account.
  2. Click inside the terminal window and execute cd.
  3. Execute wget https://cdn.cs50.net/2022/fall/实验s/6/jar.zip followed by Enter in order to 下载 a zip called jar.zip in your codespace. Take care not to overlook the space between wget and the following URL, or any other character for that matter!
  4. Now execute unzip jar.zip to create 的文件夹中名为 jar.
  5. You 不再 need the ZIP file, so you can execute rm jar.zip and respond with “y” followed by Enter at the prompt.

如何测试

Here’s how to test your code manually:

  • Define a main function in your jar.py file, wherein you 创建一个 new instance of Jar with jar = Jar(). Test this jar has the capacity it should by calling print(str(jar.capacity)). Be sure your program calls main at the bottom of the file, as via main().
  • Test that your __str__ function works as intended by calling print(str(jar)).
  • Try calling jar.deposit(2) to deposit two cookies. Calling print(str(jar)) should now show you the same number of cookies you’ve deposited.
  • Try calling jar.withdraw(1) to withdraw one cookie. Calling print(str(jar)) should now show you one fewer cookie than you had before.
  • Try experimenting with depositing and withdrawing various amounts of cookies. Are you unable to withdraw past the jar’s size? Are you unable to deposit past the jar’s capacity?

No check50 for this one!

如何提交

No need to 提交! This is a 练习题.