Building a decision tree using user inputs for ordering goods

假装没事ソ 提交于 2019-12-25 17:46:15

问题


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

回答1:


This is an example:

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



回答2:


You could write a complex decision trees using dictionaries, as netwave mentioned before

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

You should take a look at the strings methods, here are two links for python 3 that contains some tutorials and examples of string methods in python 3

Quackit.com
TutorialsPoint



来源:https://stackoverflow.com/questions/59424290/building-a-decision-tree-using-user-inputs-for-ordering-goods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!