[英]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.
我的代码中还有一个 append function 然后允许我添加产品,我还希望能够搜索我附加的产品。 I have no idea where to start any help would be greatly appreciated!
我不知道从哪里开始任何帮助将不胜感激!
I think u could try to use dict to do this thing will be a better way我认为你可以尝试使用 dict 来做这件事会是一个更好的方法
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)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.