Logic to get the lowest user read value

Asked

Viewed 364 times

1

What’s wrong with my logic? I can’t return the smallest value

def menor(size):
    size = size
    vet = [0] * size
    menor = 0

    for i in range(size):
        vet[i] = int(input('Digite os valores: '))

        if vet[i] > menor:
            menor = vet[i]

    return menor
  • If the new value is greater that the other stored value, update that other value? That’s what your code is saying, hence the error.

2 answers

3

Numbering the lines of your code for ease:

1. def menor(size):
2.     size = size
3.     vet = [0] * size
4.     menor = 0
5.     for i in range(size):
6.         vet[i] = int(input('Digite os valores: '))
7.         if vet[i] > menor:
8.             menor = vet[i]
9.     return menor
  • In line 1, a function is defined menor who gets a size size (probably integer), referring to the number of entries that will be read from the user;

  • In line 2 you define size = size, this did not make any sense. By the function receive size as a parameter, the object will already be imported into the scope and can be used within it, there is no need to reset the object, if that was the intention;

  • On line 3, you define a size vector size started with zero values. If you will not use all values read later, is there really a need to store them all? Roughly speaking, imagine a program that will read 1 million user numbers... To get the smallest, you need to know all values informed or just the smallest?

  • In line 4 you start the value of menor 0 and, due to the implemented logic, this may bring problems that I will describe later;

  • In line 5 you define a repeat loop to read all user values. This part is ok;

  • On line 6 you ask the user to enter a value and convert it to integer. Since it would not be necessary to store the values in a list, it would be sufficient that vet were an object int singular, not a list;

  • In line 7 you check whether the last reported value is greater than the current lowest value and, if the condition is met, updates the lowest value in line 8. If you want the lowest value, why update it to the highest value? This also did not make sense. Just imagine that the user typed only negative numbers: [-2, -5, -3, -9]; so the condition vet[i] > menor would never be satisfied, since menor = 0 and therefore the result would be 0. How can the smallest number of a list of negative numbers be 0 (and such value does not even belong to the list)?

  • Line 9 you return the final value of menor;

An addendum worth mentioning is that it is not recommended to use the name of a variable equal to the function name. In Python there is no distinction and when you define menor = 0, you are overwriting the function object within its scope. Although in this case it works, this is bad for maintenance and makes it impossible to use resources like recursion.

Considering the comments, the code could be:

def menor(tamanho):
    menor_valor = None
    for _ in range(tamanho):
        valor = int(input("Informe um valor: "))
        if menor_valor is None or valor < menor_valor:
            menor_valor = valor
    return menor_valor

See working on Repl.it

Initiating the value of menor_valor as None and adding the condition menor_valor is None you ensure that always the lowest value returned is a value informed by the user and, with the condition valor < menor_valor, you ensure that such value is the lowest of all.

Python natively already has a function for this: min. So your code could be just:

valores = (int(input("Informe um valor: ")) for _ in range(tamanho))
menor = min(valores)

Another important detail is that its function menor violates the principle of the uniqueness of the code, as it has more than one responsibility: read the values and identify the minor. The ideal, in most cases, would be to have one function for reading the values and another only to identify the smallest, so, instead of passing the number of values, pass an eternal object that already represents all of them.

What are "code units"?

-1

The problem is that you already initialize the lowest zero, so only if you impute negative numbers would you have a non-zero result.

Take a look at the code below:

def menor(size):
  menor = None

  if size > 0:
    vet = [random.randrange(1, 100) for x in range(size)]
    menor = vet[0]

    for n in vet:
      menor = menor if menor < n else n

  return menor

This would be a way to solve your problem, but it is not the only one, but now knowing the problem of your code you can solve your problem.

  • I didn’t understand why I was negative

  • It must have been because of the first version of his reply

  • 1

    The question code problem is not only start the value of menor with 0; the problem therefore that it operates if the reading is greater than the previously stored element is even more damaging.

  • thanks for the clarification @Jeffersonquesado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.