Building a decision tree using user inputs for ordering goods

2024/11/16 3:29:42

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

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)
https://en.xdnf.cn/q/118972.html

Related Q&A

How to de-serialize the spark data frame into another data frame [duplicate]

This question already has answers here:Explode array data into rows in spark [duplicate](3 answers)Closed 4 years ago.I am trying to de-serialize the the spark data frame into another data frame as exp…

How to pull specific key from this nested dictionary?

{"newData": [{"env1": [{"sins": [{"host": "test.com","deployTime": "2015-07-23 11:54 AM",…}],"name": “hello”}, {"…

Dropping cell if it is NaN in a Dataframe in python

I have a dataframe like this.Project 4 Project1 Project2 Project3 0 NaN laptio AB NaN 1 NaN windows ten NaN 0 one NaN NaN 1 …

How can I iterate through excel files sheets and insert formula in Python?

I get this error TypeError: Workbook object is not subscriptablewhen i run this code import xlsxwriter from openpyxl import load_workbookin_folder = rxxx #Input folder out_folder = rxxx #Output folde…

How to bind all frame widgets to Enter event

I the following code I want to bind all frame1 items to <Enter> Event, but it does not work. I mean canvas.focus_set() does not take effect. How can I solve my problem?for w in frame1.winfo_chil…

Typeerror takes no arguments [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Unable to access element within page

Form Screenshot HTML Inspect Code screenshotIm trying to access an element within a page. Cannot give out the exact page link owing to security concerns. Im writing a python program that uses selenium …

How to wtite Erlang B and Erlang C formulas in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pip is not recognizing the install command (Windows 7, Python 3.3) [duplicate]

This question already has answers here:python3 --version shows "NameError: name python3 is not defined" [duplicate](2 answers)Closed 6 years ago.I am trying to install Python programs using P…

How do I check if 1 is always followed by a 0

In Python, I cannot find a solution as to how to determine whether there is always a 0 following a 1 somewhere in a list of numbers, to form a pair 10. It doesnt have to be direct follower.For clarity,…