Fernando Gardeazabal
Fernando Gardeazabal

Reputation: 101

How to use a value in a list as a reference to another value in python?

I'm begging with python so I would like to know how to do a program that reads a code, a price and the quantity of a number of products. Then the program should print the codes of the more costly product and the code of the product with less units. Here what I tried to do:

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 already!

Upvotes: 1

Views: 68

Answers (2)

cdated
cdated

Reputation: 1773

A few items that are likely causing you trouble:

  • Price = [] should be price = []
  • Num=x should be x=10 or some other number
  • All three lines under for need to have the same indent.

To get the code with max price and code with min quantity you want to find the index of the max and min and use that on the code list:

print(code[price.index(max(price))])                                                                                                                                                                        
print(code[quantity.index(min(quantity))])

Upvotes: 1

JRazor
JRazor

Reputation: 2817

You can use zip to combine all items and max for finding the maximum item value.

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

Upvotes: 0

Related Questions