-1
I am very beginner in python programming, my doubt is the following, I need the user to type only float number, how do I do this check and report the error if it is not? I could only do if the number is integer, but float I’m having difficulties.
-1
I am very beginner in python programming, my doubt is the following, I need the user to type only float number, how do I do this check and report the error if it is not? I could only do if the number is integer, but float I’m having difficulties.
0
The best way to test is by using the method isinstance(valor, tipo)
Example:
valor = 13.3
if isinstance(valor, float):
print("é float")
else:
print("não é float")
0
One way to solve the problem is to put the user in a loop until the input is possible to be represented as a float type value.
Follow an example:
def lerNumeroFloat():
while True:
try:
numero = input("Digite um número: ")
decimalNumber = float(numero)
return decimalNumber
except:
print("Não é float. Tente novamente")
lerNumeroFloat()
-1
# Criando Looping
while True:
try:
# Caso usuario não digite um numero float gera uma exceção e executa a solicitação novamente
x = float(input("Digite um numero float: "))
break
except:
print("numero não é do tipo float")
# Validando se o numero é float
if(isinstance(x, float)):
print('é float',x)
else:
print('não é float',x)
Browser other questions tagged python float
You are not signed in. Login or sign up in order to post.
https://answall.com/q/210010/112052
– hkotsubo
This answers your question? How to check if the variable string value is number?
– Icaro Martins