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')
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"] = coldDrinkelif category == 'Cold Drink':empty_dict["category"] = 'Cold Drink'coldDrink = input("Iced Coffee or Iced Tea")empty_dict["Food"] = coldDrinkelif 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)