I am trying to write a function that takes a list of records as a parameter and finds the one with the samllest value to return in a loop.
here is my code below:
def get_min_sale_records(records):
min_price = records[0]
for min in records:
if min[3] < min_price:
min_price = min
return min_price
for this line "if min[3] < min_price:" it says that I cant use the < operator between a float and a list. I am new to python, please explain your answers.
CodePudding user response:
as someone said in the comments the bug in your code is that you compare min[3] with min_price instead of min_price[3]
def get_min_sale_records(records):
min_price = records[0]
for min in records:
if min[3] < min_price[3]:
min_price = min
return min_price
this is a working version of your code but I would recommend using the min function with key
def get_min_sale_records(records):
min(records, key = lambda r: r[3])
if you insist to use the first solution I would recommend using the name record instead of min since min is a name of a built-in function in python
def get_min_sale_records(records):
min_price = records[0]
for record in records:
if record [3] < min_price[3]:
min_price = record
return min_price