使用用户输入来订购商品的决策树 [英] Building a decision tree using user inputs for ordering goods

查看:144
本文介绍了使用用户输入来订购商品的决策树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to program a decision tree to allow customers to order goods based on their input. So far, I have devised a nested if-elif conditional structure to decide if customer want to order --> what order category --? what product from that category --> what size --> what quantity

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

Below is a sample of the structure which would become more nested if I continued the process. My question is, could this be implemented via a decision tree data structure, e.g. a dictionary which gathers user inputs, and for this to be traversed using a recursive algorithm to print the order. If so, how would this be coded?

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')


推荐答案

这是一个示例:

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

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

If you want to add the input as a key you can:

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

Idk,如果这正是您想要的,但应该给您一个提示:
另外,您在 else 之后还有一个 elif 和一个副本其他

Idk if this is what you wanted exactly but should give you an idea: Also you had an elif after else and a duplicate else in your main if/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)

这篇关于使用用户输入来订购商品的决策树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆