1

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!

3
  • What is it doing wrong? Commented Apr 18, 2016 at 0:46
  • you need to use argmax and not max. argmax is not builtin in python but you can get it from many libraries like numpy. However since you are learning, I would recommend implementing it yourself, it's a good exercise and not hard. Commented Apr 18, 2016 at 0:48
  • you should rename your question to be useful Commented Apr 18, 2016 at 1:41

2 Answers 2

1

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))])
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.