If you want the input to accept both numbers on the same line and on different lines, one option is to loop this way:
numeros = []
while True:
valores = input().split();
if not valores: # lista vazia, nenhum número foi digitado, encerrar a leitura
break
for s in valores:
try:
# tenta transformar em número
numeros.append(float(s))
except ValueError:
pass # não é número, ignorar (ou pode imprimir uma mensagem, se quiser)
When the user types something, I do the split
. If the resulting list is empty, it means that it has not typed anything (or typed only spaces), and then I exit the loop with break
.
If something was typed, I go through the values and try to turn them into numbers. If the user type something that is not a number (like abc
, for example), I am ignoring (but you could print a message, exit the loop and terminate the program, etc.). With this the input can have multiple lines, and in each row can have one or more numbers.
Then, to find the largest number, you do not need to use a recursive function (unless it is an exercise that the teacher asked to be recursive, and in this case can be based on victor’s response, which explains how to fix it). Instead, you can simply make a simple loop:
def maior(numeros):
r = numeros[0]
for n in numeros:
if n > r:
r = n
return r
Although the above loop has a redundancy: I start by initiating r
with the first element of the list and then go through the whole list, comparing each element of it to know which one is bigger. This means that in the first iteration I am comparing the first element with itself, which would be redundant. Anyway, you could start the loop from the second element with for i in range(1, len(numeros))
. And she too could return None
if the list is empty, as already suggested in victor’s response.
But if this is not an exercise that you need to manually implement the algorithm, just use the function max
:
# encontrar o maior número da lista
maior = max(numeros)
Another detail is that you are using float
to convert the string to number, which implies that the user will be able to type numbers as 1.5
, for example. But if you only want whole numbers (with no decimal places), just swap with int
.
In short, the code could look like this:
numeros = []
while True:
valores = input().split();
if not valores: # lista vazia, nenhum número foi digitado, encerrar a leitura
break
for s in valores:
try:
# tenta transformar em número
numeros.append(float(s))
except ValueError:
pass # não é número, ignorar
if numeros: # lista não é vazia
print("Maior valor encontrado: {}".format(max(numeros)))
else: # lista vazia, nem tento encontrar o maior número
print("Nenhum número foi lido")
There is no reason why the largest number should be returned in a list that only has one element, and then you should test the size of this list. Just check before if the list of all numbers is empty (if it is, then there is no reason to try to find the highest value, and it already falls right into the else
).
In the above example I used the function max
to find the largest number. But if you really want to use your own function to find the largest, and also the function that prints the number, it would look like this:
numeros = []
while True:
... igual ao código anterior
def maior(numeros):
if not numeros: # lista vazia, retornar None
return None
r = numeros[0]
for n in numeros:
if n > r:
r = n
return r
def imprimir(n):
if n is None:
print("Nenhum número foi lido")
else:
print("Maior valor encontrado: {}".format(n))
imprimir(maior(numeros))