0

I want to write a program that will let me search a set of data using multiple filters and different data types. Here I am using Breakfast food, I want to be able to specify a number of calories I want each item to be less than, then I want to pick if its red, brown, yellow or a mixture of them. It should then print out the name of each of the items which meet my criteria.

However, if for example I wanted asked for a red item of food, I wouldn't mind if it was also brown and/or yellow. I also want it to print out the name of the items which match the colour specifications and are the same or less than the number of calories I choose.

I want to be able to search for 15 different colours however, is there an easier way to do this?

So far I have written a program which works fine, I type in a number of calories and pick which of three colours I want my food to be. However, this involved writing instructions for all of the possible inputs. I want to have 15 possible colours, and this would mean a huge amount of typing to code. Also, if later on I wanted to add or delete a colour option, this would take a long time as well.

#Class definition
class Breakfast:
    def __init__(self,name,number,is_red,is_brown,is_yellow):
        self.name=name
        self.number=number
        self.is_red=is_red
        self.is_brown=is_brown
        self.is_yellow=is_yellow

#Food objects
Food_choices = [
    Breakfast("Beans",300,True,False,False),
    Breakfast("Sausage",400,False,True,False),
    Breakfast("Bacon",500,True,True,False),
    Breakfast("Toast",350,False,True,True),
    Breakfast("Tomato",800,True,False,False),
    Breakfast("Eggs",600,False,False,True),
    Breakfast("Mushrooms",150,False,True,False)
    ]

#User input
calories = input("Enter calories ")
colour_a_input = input("Is is red? ")
colour_b_input = input("Is it brown? ")
colour_c_input = input("Is it yellow? ")

#Defining variables
coloura = ""
colourb = ""
colourc = ""

#Conveting input to Boolean values
if colour_a_input == "Yes":
    coloura = True
else:
    coloura = False

if colour_b_input == "Yes":
    colourb = True
else:
    colourb = False

if colour_c_input == "Yes":
    colourc = True
else:
    colourc = False

#Search function
for Breakfast in Food_choices:
    if coloura is True:
        if colourb is True:
            if colourc is True:
                if coloura == Breakfast.is_red and colourb == Breakfast.is_brown and colourc == Breakfast.is_yellow:
                    if float(calories) >= Breakfast.number:
                        print(Breakfast.name)
            elif colourc is not True:
                if coloura == Breakfast.is_red and colourb == Breakfast.is_brown:
                    if float(calories) >= Breakfast.number:
                        print(Breakfast.name)
        elif colourb is not True:
            if colourc is True:
                if coloura == Breakfast.is_red and colourc == Breakfast.is_yellow:
                    if float(calories) >= Breakfast.number:
                        print(Breakfast.name)
            elif colourc is not True:
                if coloura == Breakfast.is_red:
                    if float(calories) >= Breakfast.number:
                        print(Breakfast.name)
    elif coloura is not True:
        if colourb is True:
            if colourc is True:
                if colourb == Breakfast.is_brown and colourc == Breakfast.is_yellow:
                    if float(calories) >= Breakfast.number:
                        print(Breakfast.name)
            elif colourc is not True:
                if colourb == Breakfast.is_brown:
                    if float(calories) >= Breakfast.number:
                        print(Breakfast.name)
        elif colourb is not True:
            if colourc is True:
                if Breakfast.is_yellow is True:
                    if float(calories) >= Breakfast.number:
                        print(Breakfast.name)
            elif colourc is not True:
                print(Breakfast.name)

Sorry for posting a whole file but I couldn't work out how to show the problem without all of it.

Duncan
  • 1

2 Answers2

0

The most object oriented way I can think of is to add a method inside the breakfast that allows you to compare different instances, you could override the operator == or just impelement a isEqual() method. Then you could create a new instance of breakfast with the data you get as input from the user and eventually simply using a for loop like:

to_match = Breakfast("*", calories, coloura, colourb, colourc) 
matches = []
for food in Food_choices:
    if food.isEqual(to_match):
        matches.append(to_match)

You could by defualt choose to have the * as a name as a matching all char. With the same logic you could also implement the >, >=, <, <= operators which could do a check on the calories.

Hope it helps.

0

Have you tried changing your variables is_colour to list of colours? It could ba a list/array of Booleans or strings. Let's say list of strings for this example. Then you could change your

colour_a_input = input("Is is red? ")
colour_b_input = input("Is it brown? ")
colour_c_input = input("Is it yellow? ")

To just one input, asking to give colours, separated by (for example) commas, like this:

input = input("In what colours is it?")

Input then should look like this:

red, brown, yellow

And split it by https://docs.python.org/3/library/stdtypes.html#str.split creating new list of input colours.

Then you could use in operator (for example 3 in [1, 2, 3] returns true, since [1, 2, 3] contains 3) to make auxiliary function like this (I got it from Checking if list is a sublist):

def sublist(lst1, lst2):
   ls1 = [element for element in lst1 if element in lst2]
   ls2 = [element for element in lst2 if element in lst1]
   return ls1 == ls2

or method that checks if input (after split()) is a sublist of a list of colours of certain food.

Finally, instead of huge number of if statements, it would look pretty much like this:

for breakfast in Food_choices:
  if sublist(input, breakfast.colours) print(breakfast.name)

There could be better approaches to this problem, but I think it would still look much better than just checking all possible outcomes with if statements, and it would also save you a lot of time, since you won't have to type all of those ifs.

VooXe
  • 52
  • 7