Know the smallest number with the function min ()

1

I want to know the least number of this list, which I present below.

I do not know where I'm fooling myself, I think I need to convert the list.

Example:

lista=[1, 3, 5]
def min(values):
    smallest = None
    for value in values:
        if smallest is None or values < smallest:
           smallest = value
    return smallest

menorvalor=min(lista)

The error you give me is this

  

TypeError: '<' not supported between instances of 'list' and 'int'

    
asked by anonymous 25.06.2018 / 18:05

1 answer

5

The problem is that you are comparing the list values and not the individual value value .

Wrong:

if smallest is None or values < smallest:
#                           ^^^ Essa é sua lista, e não o valor iterado

Fixed:

if smallest is None or value < smallest:
#                          ^ Aqui estamos comparando um por um

Standing:

lista=[1, 3, 5]
def min(values):
    smallest = None
    for value in values:
        if smallest is None or value < smallest:
           smallest = value
    return smallest

menorvalor=min(lista)

See working at IDEONE , with a more "scrambled" list to evidence the test.


To understand better

The line for value in values: "says" more or less this: "For each item in values , run the following indented code, with the respective item in the value variable at each iteration (each execution ) "

    
25.06.2018 / 18:07