3
The question says I need to receive user entries until a blank line is typed. After the user finishes the entry, I need to check in this entry if there are valid numbers and print them at the end of the program.
Follow an example of input given in the question:
1000
abacaxi
uva
15.19
-1
+1.45
123.456-
12.34+
And the expected exit:
Lista de Números Válidos Lidos = ['1000', '15.19', '-1', '+1.45']
That is, words and numbers ending with "-" and "+" are not considered valid, but numbers beginning with "-" and "+" should be considered. So far, no problem.
I used the following logic: for each item the user entered, I check if the last character is integer. If it is, enter a new list named numerosValidos
. If not, as is the case with strings and the numbers that end with signal, the program does nothing. It follows the code:
def encontraValidos(lista):
numerosValidos = []
if len(lista) == 0:
print("Lista de Números Válidos Lidos = {}".format(lista))
else:
for item in lista:
try:
if int(item[-1]):
numerosValidos.append(item)
else:
pass
except ValueError:
pass
return numerosValidos
And I get the exit:
Lista de Números Válidos Lidos = ['15.19', '-1', '+1.45']
However, as was said in the example of the question, the entry "1000"
should be on that list. Threshing the code, I realized that the "1000"
does not enter the if int(item[-1])
, but I can’t understand why.
Some help?