【问题标题】:Building a decision tree using user inputs for ordering goods使用用户输入构建决策树以订购商品
【发布时间】:2019-12-20 11:09:49
【问题描述】:

我正在尝试编写一个决策树,以允许客户根据他们的输入订购商品。到目前为止,我已经设计了一个嵌套的 if-elif 条件结构来决定客户是否要订购 --> 什么订单类别 --?该类别的产品--> 尺寸--> 数量

下面是一个结构示例,如果我继续这个过程,它会变得更加嵌套。我的问题是,这可以通过决策树数据结构来实现,例如收集用户输入的字典,并使用递归算法对其进行遍历以打印订单。如果是这样,这将如何编码?

eatOrNo = input("Type yes to eat or no to cancel")

if eatOrNo == 'yes':
    category = input('Type Hot Drink or Cold Drink or Food')
    if category == 'Hot Drink':
        hotDrink = input("Espresso or Cappucino")
    elif category == 'Cold Drink':
        coldDrink = input("Iced Coffee or Iced Tea")
    elif category == 'Food':
        coldDrink = input("Toast or Sandwich")
else:
    print('error')

elif eatOrNo == 'no':
    print('cancelled')

else:
    print('error')

【问题讨论】:

  • 正确识别,这样有点混乱。顺便说一句,您可以为此使用字典。
  • 如何为此实现字典?字典键值会根据用户输入动态生成吗?

标签: python python-3.x if-statement data-structures tree


【解决方案1】:

这是一个例子:

>>> dict = {"first":"1", "second":"2"}
>>> dict
{'first': '1', 'second': '2'}
>>> dict["first"] = 2
>>> dict
{'first': 2, 'second': '2'}

如果您想将输入添加为键,您可以:

>>> dict["third"] = "3"
>>> dict
{'first': 2, 'second': '2', 'third': '3'}

如果这正是您想要的,但应该给您一个想法: 此外,您在 else 之后有一个 elif,在您的主 if/else 中有一个重复的 else

empty_dict = {}

eatOrNo = input("Type yes to eat or no to cancel")

if eatOrNo == 'yes':
    empty_dict["eatOrno"] = "yes"
    category = input('Type Hot Drink or Cold Drink or Food')
    if category == 'Hot Drink':
        empty_dict["category"] = 'Hot Drink'
        hotDrink = input("Espresso or Cappucino")
        empty_dict["Food"] = coldDrink
    elif category == 'Cold Drink':
        empty_dict["category"] = 'Cold Drink'
        coldDrink = input("Iced Coffee or Iced Tea")
        empty_dict["Food"] = coldDrink
    elif category == 'Food':
        empty_dict["category"] = 'Food'
        coldDrink = input("Toast or Sandwich")
        empty_dict["Food"] = coldDrink
elif eatOrNo == 'no':
    print('cancelled')

else:
    print('error')


print(empty_dict)

【讨论】:

  • 谢谢。我可以用嵌套字典结构来实现它,因为我将遍历它。如果是这样,如何使用上面的代码添加它... Order --> category --> product --> size --> quantity
【解决方案2】:

您可以使用字典编写复杂的决策树,就像前面提到的netwave

cold_drinks = {"iced_coffe":"cold_drink1", "iced_tea":"cold_drink2"}
hot_drinks  = {"capuccino":"hot_drink1", "expresso":"hot_drink2"}
food = {"ham":"food1", "pizza":"food2"}
eatOrNo = input("type yes to eat no to cancel")
if eatOrNo.lower()  in ("yes",'y'):
    category = input('Type Hot Drink, Cold Drink or Food')
    if category.lower() in ("hot drink", "hot"):
        category = input("Enter the drink you want")
        if category in hot_drinks:
            ...#keep your nest behavior as you want
        else:
            print("We do not have that on menu yet!")
    elif category.lowercase() in ("cold drink", "cold"):
        ... #repeat the same as in hot drink
    elif category.lowercase() == "food":
        ...
elif eatOrNo == 'no':
    print('cancelled')
else:
    print("error")

你应该看看字符串方法,这里有两个python 3的链接,其中包含一些教程和python 3中字符串方法的示例

Quackit.com
TutorialsPoint

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 2018-09-20
    • 2011-06-12
    • 2019-05-29
    • 2019-07-01
    • 2021-03-02
    • 2017-01-29
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多