The End

烤地瓜

- 2 min

烤地瓜高清复刻版


目标:烤地瓜

定义地瓜的状态

  • 随烧烤时间改变,地瓜的状态发生变化
    • 0-3 生的
    • 3-6 半生不熟
    • 6-8 熟了
    • > 8 烤糊了
class SweetPotato:
    """
    地瓜类
    """

    def __init__(self, num: int) -> None:
        """
        Initialize a SweetPotato object.

        Args:
            num (int): The potato number.
        """
        # Define attributes
        self.cooked_state: str = "生的"  # Potato state
        self.cooked_time: int = 0  # Total cooking time
        self.num: int = num  # Potato number
        self.condiments: List[str] = []  # List of condiments

    def cook(self, time: int) -> None:
        """
        Cook the potato for a given time.

        Args:
            time (int): The time to cook the potato.

        Returns:
            None
        """
        self.cooked_time += time
        if 0 <= self.cooked_time < 3:
            self.cooked_state = "生的"
        elif 3 <= self.cooked_time < 6:
            self.cooked_state = "半生不熟"
        elif 6 <= self.cooked_time < 8:
            self.cooked_state = "熟了"
        else:
            self.cooked_state = "烤糊了"

    def add_condiment(self, condiment: str) -> None:
        """
        Add a condiment to the list of condiments.

        Args:
            condiment (str): The condiment to add.

        Returns:
            None
        """
        self.condiments.append(condiment)

    def __str__(self) -> str:
        """
        Convert the SweetPotato object into a string representation.

        Args:
            self: The SweetPotato object.

        Returns:
            str: A string representation of the SweetPotato object, including its number, cooked state, and condiments.
        """
        return f"地瓜{self.num}的状态: {self.cooked_state}, 佐料: {self.condiments}"

创建对象

sweetpotato1 = SweetPotato(1)

执行烧烤操作

sweetpotato1.cook(2)
sweetpotato1.add_condiment("酱油")
print(sweetpotato1)
sweetpotato1.cook(3)
sweetpotato1.add_condiment("盐")
print(sweetpotato1)
sweetpotato1.cook(1)
sweetpotato1.add_condiment("辣酱")
print(sweetpotato1)
sweetpotato1.cook(2)
print(sweetpotato1)

创建对象2

sweetpotato2 = SweetPotato(2)

再次烧烤

sweetpotato2.cook(1)
sweetpotato2.add_condiment("番茄酱")
print(sweetpotato2)
sweetpotato2.cook(5)
sweetpotato2.add_condiment("沙拉")
print(sweetpotato2)
sweetpotato2.cook(4)
sweetpotato2.add_condiment("芥末")
print(sweetpotato2)

完成