【问题标题】:Creating coffee class. Important function is __init__ and method simulates coffee brewing [closed]创建咖啡课。重要的功能是 __init__ 和方法模拟咖啡冲泡 [关闭]
【发布时间】:2018-03-21 14:05:20
【问题描述】:

此方法模拟冲泡咖啡的动作。只有当咖啡机有水、过滤器、咖啡渣、咖啡壶是空的且咖啡机当前处于关闭状态时,才能进行此过程。 此过程的结果是水箱变空,咖啡壶设置为与水箱中相同的量,机器保持开启,咖啡壶不干净。

class Coffee:
    def __init__(self):
        self.refill= 20
        self.watertank=20
        self.filter= False
        self.on=False
        self.spoons=4
        self.machine=False
        self.pot= 0
def brew(self): # And as requirment
        if (machine == self.watertank and machine == self.filter \
        and machine == self.spoons and machine == self.pot \
        and machine == self.on):
            print("The coffee is brewing")
            while self.on == True:
                if self.watertank < 20:
                    if self.pot==self.watertank:
                        return False

这些功能必须有效。我对我正在尝试做的事情的解释是我在构造函数机器中创建了变量以定义为真。如果 machine 为 True,过滤 True,spoons ==4,pot ==0,self.on 为 False。结果将是

水箱 ==0, pot== 水箱, self.on == True, pot ==0

你们能看看我的功能吗?我可以做什么样的改变?我添加了所有命令而不是 And。你们能不能让我们看看构造函数来更多地理解这个方法。

【问题讨论】:

  • 您收到什么错误或问题?您还没有说明上面的代码有什么问题,这可能就是有人对您的帖子投反对票的原因
  • 进一步扩展——我们要求任何包含代码的问题都具有可能的最短代码,这允许其他人产生特定错误 (它本身应该包含在问题中)。请参阅minimal reproducible example 定义——因此您应该 (1) 显示特定错误,并且 (2) 提供您测试过的最短代码,以在单独运行时产生相同的错误。

标签: python computer-science


【解决方案1】:

嗯,有几件事跳出来了。

  1. 您不会在任何地方提高self.pot 的级别或降低self.watertank 的级别。

  2. 咖啡机永远不会打开,因此您的冲泡if 语句永远不会被执行。

  3. 酝酿中的if 语句比较了没有意义的事物。例如,您为什么将machineself.watertank 进行比较?这两个属性不相关,因为一个是整数,另一个是布尔值。

  4. 有几个属性可以/应该包含在每个对象的单独类中,例如 Pot 的单独类和 Watertank 的另一个类,它包含 original_level 等属性并具有 @987654332 的方法@等

  5. 向您的类添加帮助方法以加载勺子、安装过滤器、打开/关闭机器等也是有用的补充,这样您的类就可以轻松地进行交互。

    李>
  6. 我创建了一个 Container 抽象类,它永远不会被直接使用,而是应该被另一个类扩展。这个抽象类将包含实现它的每个类的基本功能。

import abc

class Container (object):
    __meta__ = abc.ABCMeta

    level           = None
    original_level  = None

    @abc.abstractmethod
    def increase (self, amt=1):
        self.level = self.level + amt

    @abc.abstractmethod
    def decrease (self, amt=1):
        self.level = self.level - amt

    @abc.abstractmethod
    def is_empty(self):
        return self.level == 0


class Pot (Container):
    def __init__(self, level):
        self.level          = level
        self.original_level = level

class Watertank (Container):
    def __init__(self, level):
        self.level          = level
        self.original_level = level

class Coffee:
    def __init__(self):
        self.refill     = 20
        self.watertank  = Watertank(20)
        self.pot        = Pot(0)    
        self.filter     = False
        self.on         = False
        self.spoons     = 0

    def brew(self): # And as requirment
        if self.is_ready():
            print("The coffee is brewing...")

            while self.on :             
                self.watertank.decrease( 1 )
                self.pot.increase( 1 )

                if self.pot.level == self.watertank.original_level and self.watertank.is_empty() :
                    print("The coffee has been brewed. Enjoy!")
                    break
        else :
            print("Sorry, the brewer is not ready to brew.")
            self.ready_check()

    def ready_check(self):
        if self.watertank.level == 0 :
            print( "The Watertank is not filled." )
        if not self.filter :
            print( "A filter has not been loaded." )
        if self.spoons == 0 :
            print( "There are no spoons of coffee mix." )
        if self.pot.level > 0 :
            print( "The Pot is not empty." )
        if not self.on:
            print( "The brewer is not turned on." )

    def is_ready(self):
        return  self.watertank.level > 0 \
            and self.filter \
            and self.spoons > 0\
            and self.pot.level == 0 \
            and self.on

    def add_spoons(self, amt=1):
        self.spoons = self.spoons + amt

    def install_filter(self):
        self.filter = True
        print("A filter has been installed.")

    def turn_on (self):
        self.on = True
        print("The brewer is now on!")

    def turn_off (self):
        self.on = False
        print("The brewer is now off!")


if __name__ == "__main__":
    brewer = Coffee()
    brewer.install_filter()
    brewer.add_spoons(4)
    brewer.turn_on()

    brewer.brew()

另外,我非常无聊,继续探索这个用例。我添加了一个咖啡师,他可以通过询问一些关于偏好的问题来个性化您的咖啡订单。要点如下:https://gist.github.com/sadmicrowave/73541a76f8133be09c8318c0770c5343

【讨论】:

  • 对于一个没有明确指定具体问题的问题,这是一个很大的努力;虽然您已经明确为 OP 提供了一项服务,但值得注意的是,How to Answer 明确建议反对回答此类问题,直到问题得到澄清(请注意“回答好问题”部分)。
  • 感谢您的意见。我很无聊,喜欢上课;所以我想我会试一试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 2018-11-06
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2016-01-08
相关资源
最近更新 更多