Cara menggunakan python cookiejar

Suppose that you’d like to implement a cookie jar in which to store cookies. In a file called jar.py, implement a class called Jar with these methods:

Show
  • __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
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    0, though, __init__ should instead raise a
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    2.
  • from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    3 should return a
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    4 with \(n\)
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    5, where \(n\) is the number of cookies in the cookie jar. For instance, if there are 3 cookies in the cookie jar, then
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    4 should return
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    7
  • from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8 should add
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    9 cookies to the cookie jar. If adding that many would exceed the cookie jar’s capacity, though,
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8 should instead raise a
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    2.
  • check50 cs50/problems/2022/python/jar
    
    2 should remove
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    9 cookies from the cookie jar. Nom nom nom. If there aren’t that many cookies in the cookie jar, though,
    check50 cs50/problems/2022/python/jar
    
    2 should instead raise a
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    2.
  • capacity should return the cookie jar’s capacity.
  • check50 cs50/problems/2022/python/jar
    
    7 should return the number of cookies actually in the cookie jar.

Structure your class per the below. You may not alter these methods’ parameters, but you may 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):
        ...

Either before or after you implement jar.py, additionally implement, in a file called

submit50 cs50/problems/2022/python/jar
0, four or more functions that collectively test your implementation of Jar thoroughly, each of whose names should begin with
submit50 cs50/problems/2022/python/jar
2 so that you can execute your tests with:

Note that it’s not as easy to test instance methods as it is to test functions alone, since instance methods sometimes manipulate the same “state” (i.e., instance variables). To test one method (e.g.,

check50 cs50/problems/2022/python/jar
2), then, you might need to call another method first (e.g.,
from jar import Jar


def test_init():
    ...


def test_str():
    jar = Jar()
    assert str(jar) == ""
    jar.deposit(1)
    assert str(jar) == "🍪"
    jar.deposit(11)
    assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"


def test_deposit():
    ...


def test_withdraw():
    ...
8). But the method you call first might itself not be correct!

And so programmers sometimes mock (i.e., simulate) state when testing methods, as with Python’s own mock object library, so that you can call just the one method but modify the underlying state first, without calling the other method to do so.

For simplicity, though, no need to mock any state. Implement your tests as you normally would!

Hints

from jar import Jar


def test_init():
    ...


def test_str():
    jar = Jar()
    assert str(jar) == ""
    jar.deposit(1)
    assert str(jar) == "🍪"
    jar.deposit(11)
    assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"


def test_deposit():
    ...


def test_withdraw():
    ...

Demo

You’re welcome, but not required, to implement a

submit50 cs50/problems/2022/python/jar
5 function, so this is all we can demo!

Cara menggunakan python cookiejar

Source: Sesame Street

Before You Begin

Log into code.cs50.io, click on your terminal window, and execute

submit50 cs50/problems/2022/python/jar
6 by itself. You should find that your terminal window’s prompt resembles the below:

Next execute

to make a folder called

submit50 cs50/problems/2022/python/jar
7 in your codespace.

Then execute

to change directories into that folder. You should now see your terminal prompt as

submit50 cs50/problems/2022/python/jar
8. You can now execute

to make a file called jar.py where you’ll write your program. You can also execute

to create a file called

submit50 cs50/problems/2022/python/jar
0 where you can write tests for your program.

How to Test

Here’s how to test your code manually:

  • Open your
    submit50 cs50/problems/2022/python/jar
    
    0 file and import your Jar class with jar.py3. Create a function called jar.py4, wherein you create a new instance of Jar with jar.py6. jar.py7 that this jar has the capacity it should, then run your tests with jar.py8.
  • Add another function to your
    submit50 cs50/problems/2022/python/jar
    
    0 file called class0. In class0, create a new instance of your Jar class and
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8 a few cookies. jar.py7 that class5 prints out as many cookies as have been
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8ed, then run your tests with jar.py8.
  • Add another function to your
    submit50 cs50/problems/2022/python/jar
    
    0 file called class9. In class9, create a new instance of your Jar class and
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8 a few cookies. jar.py7 that the jar’s
    check50 cs50/problems/2022/python/jar
    
    7 attribute is as large as the number of cookies that have been
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8ed. Also jar.py7 that, if you deposit more than the jar’s capacity,
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8 should raise a
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    2. Run your tests with jar.py8.
  • Add another function to your
    submit50 cs50/problems/2022/python/jar
    
    0 file called __init__2. In __init__2, create a new instance of your Jar class and first
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    8 a few cookies. jar.py7 that
    check50 cs50/problems/2022/python/jar
    
    2ing from the jar leaves the appropriate number of cookies in the jar’s
    check50 cs50/problems/2022/python/jar
    
    7 attribute. Also jar.py7 that, if you withdraw more than the jar’s
    check50 cs50/problems/2022/python/jar
    
    7,
    check50 cs50/problems/2022/python/jar
    
    2 should raise a
    from jar import Jar
    
    
    def test_init():
        ...
    
    
    def test_str():
        jar = Jar()
        assert str(jar) == ""
        jar.deposit(1)
        assert str(jar) == "🍪"
        jar.deposit(11)
        assert str(jar) == "🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪"
    
    
    def test_deposit():
        ...
    
    
    def test_withdraw():
        ...
    
    2. Run your tests with jar.py8.

You can execute the below to check your code using capacity4, a program that CS50 will use to test your code when you submit. But be sure to test it yourself as well!

check50 cs50/problems/2022/python/jar

Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that capacity4 outputs to see the input capacity4 handed to your program, what output it expected, and what output your program actually gave.