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.