python – How to get the lowest value in a list?


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

The exercise is as follows:

Write a function called fetchSmallest() that takes a list of integers and returns the smallest 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 performed using a repetition structure, and the use of the min() function is prohibited. Example:

In: 7.0 5.5 6.0 9.0 Out: 5.5

What have I done so far:

listaInteiros = []
i = 0

while i < 4:
    inteiros = int(input())
    i += 1

def buscarMenor():

I'm stuck in the function, give me a hand please!



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 it would be:

  • define the function
  • give the greatest possible value in the declaration/attribution of i
  • iterate the list numbers
  • if the iterated number is less than i , replace the i with that

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

Scroll to Top