需要帮助:TypeError:&#39;在<string>&#39;需要字符串作为左操作数,而不是列表

时间:2016-03-14 04:27:38

标签: python-3.x

我正在尝试让我的代码向我提供信息,例如我输入品牌的价格&#34; Nike&#34;或类似的东西。但是,我得到错误代码作为标题。如有任何帮助,将不胜感激

clothing_brands = [
["544", "Jordan", "Shoes", 200],
["681", "Stussy", "Sweatshirt", 50],
["481", "Obey", "T-Shirt", 30],
["339", "North Face", "Jacket", 80],
["250", "Levis", "Jeans", 40],
["091", "Nike", "Socks", 10],
]

def findClothing(brand):
    found = False
    for brand in clothing_brands:
        if clothing_brands in brand[1]:
            found = True
            break
    if found:
        return brand
    else:
        return None

def printBrand(brand):
    print ("\n~~~~Thrift Shop Brands:~~~~")
    print ("Item Number:", brand[0])
    print ("Brand Name:", brand[1])
    print ("Clothing Type:", brand[2])
    print ("Price:", brand[3])

if __name__ == "__main__":
    print ("\n~~~Welcome to Macklemore's clothing Thrift Shop~~~~")
    print ("1) Add a brand you would like to see us carry")
    print ("2) What is the brand's clothing type?")
    print ("3) What is the price of a particular brand?")
    print ("4) What is a brand's information?")
    print ("5) Print everything we have in stock")
    print ("0) Exit")

    while True:
        option = input ("Please Select a Menu item.")

        if (option == "0"):
            break

        if (option == "1"):
            Item_number = input ("What is the Item Number?")
            Brand_name = input ("What is the name of the Brand?")
            Clothing_type = input ("What type of clothing is the Brand?")
            Price = input ("How much is the item?")
            brand = [Item_number, Brand_name, Clothing_type, Price]
            clothing_brands.append(brand)
            print ("Thank you for your input, We will begin searching to add that item into our inventory")
            print (Brand_name, Clothing_type, Price)
            print ("Come back in a couple of days to see if we have added the item")

        if (option == "2"):
            brand = input ("What is the brand you are looking for information on?")
            brand = findClothing(brand)
            if (brand != None):
                print ("The Clothing type of this brand is:", brand[2])

        if (option == "3"):
           brand = input ("What is the brand you are looking for information on?")
           brand = findClothing(brand)
           if (brand != None):
                print ("The Price of the brand is:", brand[3])

        if (option == "4"):
            brand = input ("What is the brand you are looking for information on?")
            brand = findClothing(brand)
            if (brand != None):
                printBrand (brand)
            else:
                print ("Sorry, we do not carry this brand. If you would like for us to search for this brand, Please try option 1!")

        if (option == "5"):
            for brand in clothing_brands:
                print (brand)

1 个答案:

答案 0 :(得分:1)

您的findClothing方法不使用其输入参数(它被循环变量覆盖)而是使用if clothing_brands in brand[1]:,这是没有意义的,因为clothing_brands甚至不是字符串。

修复可以解决您的错误;

def findClothing(input_brand):
    found = False
    for brand in clothing_brands:
        if input_brand in brand[1]:
            found = True
            break
    if found:
        return brand
    else:
        return None