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"?
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.
– Jefferson Quesado