ghostdad2
ghostdad2

Reputation: 1

Printing from multiple lists with the same index number

hatType = ["Cap" , "Beanie" , "Top Hat"]
hatColour = ["Blue" , "Brown" , "Black"]
hatPrice = [12 , 13 , 14]
hatQuantity [ 5 , 7 , 3]

I want to create an input that asks the user for the hat type and then printing everything with the same index. I also have an append function in my code then allows me to add products and I also want to be able to search for the ones I have appended. I have no idea where to start any help would be greatly appreciated!

Upvotes: 0

Views: 1531

Answers (2)

Dicsok Gabriel
Dicsok Gabriel

Reputation: 13

hatType = ["Cap" , "Beanie" , "Top Hat"]
hatColour = ["Blue" , "Brown" , "Black"]
hatPrice = [12 , 13 , 14]
hatQuantity =[ 5 , 7 , 3]

ListOfLists=[hatType,hatColour,hatPrice, hatQuantity]# list of All the lists for easier printing
print('Chose your hat from these types: ',hatType)
type = input() #Gets input from keyboard ,here you specify the type of hat you want

try:
    index = hatType.index(type) #Returns the index for the hat type specified if hattype not found returns error
    for list in ListOfLists: 
        print(list[index]) #prints the item from every list with same index
except:
    print("Hat type not found") #index = hatType.index(type) can return a error if type is not found in list so we catch the error and print that we don t have that hat type

Upvotes: 1

pepper
pepper

Reputation: 413

I think u could try to use dict to do this thing will be a better way

def search(target_type, types, colour, price, quantity):
    for i in range(len(types)):
        cur_type = types[i]
        if cur_type == target_type:
            print(types[i])
            print(colour[i])
            print(price[i])
            print(quantity[i])
            return
    print('not found')

def add(h_type, h_color, h_price, h_quantity, types, colour, price, quantity):
    types.append(h_type)
    colour.append(h_color)
    price.append(h_price)
    quantity.append(h_quantity)
    return types, colour, price, quantity

if __name__ == '__main__':
    types = ["Cap", "Beanie", "Top Hat"]
    colour = ["Blue", "Brown", "Black"]
    price = [12, 13, 14]
    quantity = [5, 7, 3]
    search('Cap', types, colour, price, quantity)
    print('----'*10)
    types, colour, price, quantity = add('Cap2',"Blue2",12,5,types, colour, price, quantity)
    search('Cap2', types, colour, price, quantity)

Upvotes: 0

Related Questions