vendredi 20 décembre 2019

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

Aucun commentaire:

Enregistrer un commentaire