Numbering the lines of your code to make it easier:
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 menor
function is defined that receives a size
(probably integer) size, referring to the number of entries that will be read from the user;
-
On line 2 you set size = size
, this did not make any sense. Because the function receives size
as a parameter, the object will already be imported into the scope and can be used within it, there is no need to redefine the object, if that was the intention;
-
In line 3, you define a vector of size size
started with zero values. If you will not use all values read later, is there even a need to store them all? Roughly speaking, imagine a program that will read 1 million user numbers ... To get the lowest, do you need to know all the reported values or just the smallest?
-
In line 4 you start the value of menor
to 0 and, due to the implemented logic, this can cause problems that I will describe later;
-
On line 5 you define a loop repetition to read all the values of the user. This part is ok;
-
On line 6 you prompt 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 suffice that vet
was a int
object, not a list,
-
On line 7 you check if the last value entered is greater than the current lower value and, if the condition is satisfied, update the lower value on line 8. If you want the lowest value, why update it for the greater? That also did not make sense. Just imagine the user typing only negative numbers: [-2, -5, -3, -9]
; so that the vet[i] > menor
condition would never be satisfied since menor = 0
and therefore the result would be 0. How can the least number of a list of negative numbers be 0 (and such a value does not even belong to the list)? >
-
Line 9 you return the final value of menor
;
An addendum that is 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 set menor = 0
, you are overwriting the function object within the scope of the function. Although in this case it works, this is bad for maintenance and makes it impossible to use features 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 at Repl.it
Starting the value of menor_valor
as None
and adding the condition menor_valor is None
you ensure that always the smallest value returned is a value entered by the user and, with the valor < menor_valor
condition, you guarantee that such value is the least of them all.
Natively Python already has a function for this: min
. So your code could just be:
valores = (int(input("Informe um valor: ")) for _ in range(tamanho))
menor = min(valores)
Another important detail is that its menor
function hurts the principle of uniqueness of the code, since it has more than one responsibility: to read the values and to identify the minor. The ideal, in most cases, would be to have a function to read the values and another only to identify the smaller, so, instead of passing the number of values, pass an iterable object that already represents all of them. >
What are "code units"?