如何在一个类中搜索多个变量并打印另一个?有没有可能让这更简单?

2024-10-05 10:47:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个程序,让我可以使用多个过滤器和不同的数据类型搜索一组数据。这里我使用的是早餐食品,我想能够指定我希望每种食物的卡路里数,然后我想选择它是红色、棕色、黄色还是它们的混合物。然后,它应该打印出符合我的条件的每个项目的名称

然而,如果我想要一个红色的食物,我不会介意它是否也是棕色和/或黄色的。我还希望它打印出的项目名称,符合颜色规格,是相同的或少于我选择的卡路里数

我想能够搜索15种不同的颜色,但是,有没有更简单的方法来做到这一点

到目前为止,我已经写了一个很好的程序,我输入了大量的卡路里,然后从三种颜色中选择我想要的食物。然而,这涉及到为所有可能的输入编写指令。我想要15种可能的颜色,这意味着需要大量的输入代码。此外,如果以后我想添加或删除颜色选项,这也需要很长时间

#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)

很抱歉发布了一个完整的文件,但如果没有所有的文件,我无法解决如何显示问题


Tags: namefalsetruenumberinputifisred
2条回答

您是否尝试过将变量is_colour更改为颜色列表?它可以是布尔或字符串的列表/数组。让我们假设本例中的字符串列表。然后你可以改变你的想法

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

对于一个输入,要求给出颜色,用逗号分隔(例如),如下所示:

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

然后输入应如下所示:

red, brown, yellow

并通过https://docs.python.org/3/library/stdtypes.html#str.split创建新的输入颜色列表将其拆分

然后您可以使用in运算符(例如3 in [1, 2, 3]返回true,因为[1, 2, 3]包含3)来生成这样的辅助函数(我从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

或者检查输入(在split()之后)是否是特定食物颜色列表的子列表的方法

最后,与大量的if语句不同,它看起来非常像这样:

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

可能有更好的方法来解决这个问题,但我认为它看起来仍然比用if语句检查所有可能的结果要好得多,而且它也会为您节省很多时间,因为您不必键入所有这些if

我能想到的最面向对象的方法是在早餐中添加一个方法,它允许您比较不同的实例,您可以重写操作符==或只强制一个isEqual()方法。然后,您可以使用从用户获得的输入数据创建一个新的早餐实例,并最终简单地使用for循环,如:

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

您可以通过取消选择将*作为名称作为匹配的所有字符。使用相同的逻辑,您还可以实现>>=<<=操作符,这些操作符可以检查卡路里

希望能有帮助

相关问题 更多 >

    热门问题