How to get the lowest value in a list?

3

I'm stuck in this program, I can not create a function in Python.

The exercise is as follows:

  

Write a function called getMenor () that takes a list of integers and returns the least of the elements. Also, write the main program that passes the list to the function and displays the return. The search for the smallest element must be done using repetition structure, and the use of the min () function is forbidden. Example:

Input: 7.0 5.5 6.0 9.0 Output: 5.5

What I've done so far:

listaInteiros = []
i = 0

while i < 4:
    inteiros = int(input())
    listaInteiros.append(inteiros)
    i += 1
print(listaInteiros)

def buscarMenor():

I'm stuck in the role, please give me some strength!

    
asked by anonymous 30.06.2017 / 00:39

2 answers

2

Suggestion:

def buscarMenor(lst):
    i = float("inf")
    for nr in lst:
        if nr < i:
            i = nr
    return i

listaInteiros = [7, 5, 6, 9]
menor = buscarMenor(listaInteiros)
print(menor) # 5

listaDecimais = [7.0, 5.5, 6.0, 9.0]
menor = buscarMenor(listaDecimais)
print(menor) # 5.5

By steps would be:

  • Define function
  • give as much value as possible in the declaration / assignment of i
  • iterate the numbers of the lists
  • If the iterated number is less than i , replace it with that

Then to run, just call the function with a list of numbers and save its return in a variable.

    
30.06.2017 / 00:57
0
def buscarMenor(lista):
    menor = lista[0]
for i in lista:
    if i < menor:
        menor = i
return menor
print(buscarMenor([7.0,5.5,6.0,9.0]))
    
06.07.2018 / 15:21