#πŸ”’ Classes

49 messages Β· Page 1 of 1 (latest)

sudden fable
#

Im new to classes, how do pass my checks below

class Jar:
    def __init__(self, capacity=12):
        if capacity < 0:
            raise ValueError
        self.capacity = capacity


    def __str__(self):
        for n in self.capacity:
            return print(":cookie:", end=end)

    def deposit(self, n):
        n = input("add cookies to jar")
        self.capacity += n

        if self.capacity >= 12:
            raise ValueError

    def withdraw(self, n):
        ...

    @property
    def capacity(self):
        return print("12")

    @property
    def size(self):
        return print(self.capacity)
rotund urchinBOT
#

@sudden fable

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sudden fable
#

not sure if my code is correct too

indigo cosmos
#

You’re mixing up a few concepts:

  • capacity should be the maximum cookies the jar can hold.

  • You also need to keep track of the current number of cookies (e.g. self._size).

  • @property should return a value, not print something.

  • str should return a string representation of the jar.
    here is a clean version:
    class Jar:
    def init(self, capacity=12):
    if capacity < 0:
    raise ValueError("Capacity cannot be negative")
    self._capacity = capacity
    self._size = 0 # current cookies in the jar

    def str(self):
    return "cookie" * self._size

    def deposit(self, n):
    if self._size + n > self.capacity:
    raise ValueError("Too many cookies for the jar")
    self._size += n

    def withdraw(self, n):
    if n > self._size:
    raise ValueError("Not enough cookies to withdraw")
    self._size -= n

    @property
    def capacity(self):
    return self._capacity

    @property
    def size(self):
    return self._size

sudden fable
#

ok thanks for the advice

#

when i first intialise jar, it should be empty

vivid wave
#
def __str__(self):
    for n in self.capacity:
        return print(":cookie:", end=end)
#

you're returning a print

#

do you information about what they expect you to return from __str__?

sudden fable
#

str should return a str with 𝑛 πŸͺ, where 𝑛 is the number of cookies in the cookie jar. For instance, if there are 3 cookies in the cookie jar, then str should return "πŸͺπŸͺπŸͺ"

vivid wave
#

you don't need a loop to do that then

#

you can multiply a string

sudden fable
#

ait thx

#

and what if its empty?

vivid wave
#

any string multiplied by 0 will return an empty string

#

no special case needed

sudden fable
#

oh ok

#

thanks

indigo cosmos
#

Final version for str:
def str(self):
return "πŸͺ" * self._size

vivid wave
sudden fable
vivid wave
#

100%

sudden fable
#

damn

#

assertion Error?

#
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():
    ...
#

my test

vivid wave
sudden fable
#
class Jar:
    def init(self, capacity=12):
        if capacity < 0:
            raise ValueError("Capacity cannot be negative")
        self.capacity = capacity
        self.size = 0  # current cookies in the jar

    def str(self):
        return "πŸͺ" * self.size

    def deposit(self, n):
        if self._size + n > self.capacity:
            raise ValueError("Too many cookies for the jar")
        self._size += n

    def withdraw(self, n):
        if n > self.size:
            raise ValueError("Not enough cookies to withdraw")
        self._size -= n

    @property
    def capacity(self):
        return self.capacity

    @property
    def size(self):
        return self.size

vivid wave
#

you need __str__ for it to work

indigo cosmos
vivid wave
#

not def str

sudden fable
#

oh man

vivid wave
rotund urchinBOT
#

10. Do not copy and paste answers from ChatGPT or similar AI tools.

sudden fable
#

am i not setting it here?

#
class Jar:
    def __init__(self, capacity=12):
        if capacity < 0:
            raise ValueError("Capacity cannot be negative")
        self.capacity = capacity
        self.size = 0  # current cookies in the jar

    def __str__(self):
        return "πŸͺ" * self.size

    def deposit(self, n):
        if self._size + n > self.capacity:
            raise ValueError("Too many cookies for the jar")
        self._size += n

    def withdraw(self, n):
        if n > self.size:
            raise ValueError("Not enough cookies to withdraw")
        self._size -= n

    @property
    def capacity(self):
        return self.capacity

    @property
    def size(self):
        return self.size
vivid wave
#

you need to be using _ for your attributes since you're turning them into properies

#

otherwise they will overwrite each other

#

does it indicate what you're meant to use the properties for?

sudden fable
#

not much

#

ok thanks

#

!close

rotund urchinBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.