我正在用python乞求,所以我想知道如何编写一个程序来读取代码,价格和一些产品的数量。 然后程序应该用较少的单位打印更昂贵的产品的代码和产品的代码。 这就是我试图做的事情:
Price=[]
code=[]
quantity=[]
Num=x
for i in range(x):
code.append(input("product code: "))
price.append(float(input("price: ")))
quantity.append(int(input("quantity: ")))
print(code[max(price)])
print(code[min(quantity)])
ThAnks已经!
答案 0 :(得分:1)
可能会给您带来麻烦的一些项目:
Price = []
应为price = []
Num=x
应为x=10
或其他数字for
下的所有三行都需要具有相同的缩进。要获取具有最大价格的代码和具有最小数量的代码,您需要找到max和min的索引并在代码列表中使用该代码:
print(code[price.index(max(price))])
print(code[quantity.index(min(quantity))])
答案 1 :(得分:0)
您可以使用zip
组合所有项目,max
查找最大项目值。
all_items = zip(price, code, quantity)
item_with_max_price = max(all_items, key=lambda x: x[0])
item_with_max_quantity = max(all_items, key=lambda x: x[2])