Skip to content Skip to sidebar Skip to footer

Building A Decision Tree Using User Inputs For Ordering Goods

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 w

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

Solution 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 wantelse:
            print("We do not have that on menu yet!")
    elif category.lowercase() in ("cold drink", "cold"):
        ... #repeat the same as in hot drinkelif 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.comTutorialsPoint

Post a Comment for "Building A Decision Tree Using User Inputs For Ordering Goods"