They actually had some really good, useful and functional responses so I’m just going to say some things again that are the same as others
1 - If you set the input to float, you cannot receive a string because when the message is received, it will be converted, but it would be wrong to try to convert ".". The best solution would be to check if it is a number later
2 - You could have simply put as an infinite loop to be able to decrease the program size, but it is not required
3 - I don’t know if this is from your IDE, but keep an eye out because in python it is not used () when it will give a condition "while (i!='.'):" this in python does not happen
No more bullshit, I’ll show you how my solution turned out:
Only broken value
lista=[]
while True:
i = input('Digite um número (ou "." para encerrar): ').replace(',','.')
"""Aquele replace caso tenha uma , ele irá substituir por . para poder converter
para float"""
if i[0].isnumeric() and '.' in i:
#Aqui especificamos que para poder ser adicionado é apenas um número quebrado
#Se digitar um inteiro não será adicionado
#O .isnumeric vai verificar se a primeira palavra (i[0]) é um número
i = float(i)
lista.append(i)
else:
if i=='.':
print(lista)
exit('Obrigado por usar o programa!')
#uma forma mais rápida de sair do programa sem usar print
else:
print('Por favor digite algo válido')
Any number
lista=[]
while True:
i = input('Digite um número (ou "." para encerrar): ').replace(',','.')
if i[0].isnumeric():
#Mas para ser adicionado ainda é necessário que pelo menos comece com um número
i = float(i)
lista.append(i)
else:
if i=='.':
print(lista)
break
else:
print('Por favor digite algo válido')
print('Obrigado por usar o programa')
#apenas para demonstrar a funcionalidade do exit()
The failure of this program is if someone type a number and then letters, to prevent it from giving problem, you could use Try:
Make mistakes impossible
lista=[]
while True:
i = input('Digite um número (ou "." para encerrar): ').replace(',','.')
if i[0].isnumeric():
try:
i = float(i)
lista.append(i)
except:
print('Digite um número válido')
else:
if i=='.':
print(lista)
break
else:
print('Por favor digite apenas números válidos')
print('Obrigado por usar o programa')
our thank you very much, that’s exactly what I was needing, I didn’t know the use of isnumeric so I wasn’t getting.
– Jedi Negão
There are other ways to detect whether the value passed is a number or not, but the most viable to date is the . isnumeric(), there are many things in python that make our work easier, and this is one of them haha
– Paulo Thiago
@Paulo Thiago, the input that captures the value of the variable
i
does not need the functionstr
. Because, the input already passes the entered value as string. In this case, the functionstr
becomes redundant. Hug.– Solkarped
@Jedinegão Just one detail: there are several characters for which
isnumeric
returnsTrue
but error when converting tofloat
, see here. You’d better wear it soonfloat
and capture theValueError
to know if a number has not been entered (as I did in my answer)– hkotsubo
@Solkarped I used the
str(input())
just to clarify and make it clear to @Jedi Black Guy, but overall it’s less redundant to just use theinput()
, as you said yourself. Hug– Paulo Thiago
@hkotsubo For this situation the .
isnumeric()
served well, but it’s like you said, there are several values that he ends up returningTrue
, so your answer turns out to be also very viable– Paulo Thiago
@Paulothiago It will only serve well if the user does not type any of these problematic characters I mentioned. All right that are not very common characters in our day-to-day, but if you are reading user entries, you better be prepared to receive "anything" :-)
– hkotsubo