Simple attempt to transform into float (with this you can know if the typed string has the same shape as a float):
h = input("Informe sua altura: ")
try:
float(h)
print('É float, altura: ' ,h)
except ValueError as err:
print('Não é float')
With repetition of input
, function-free:
while True:
h = input("Informe sua altura: ")
try:
float(h)
break
except ValueError as err:
print('formato errado')
print('altura:', h)
With repetition of input
, with recursive function:
def return_float():
h = input("Informe sua altura: ")
try:
return float(h)
except ValueError as err:
print('Formato errado, por favor tente outravez')
return return_float()
print('a sua altura é', return_float())
Finally to follow the logic of yours and for what you asked me in comment, "...I can not use neither Try nor except...", you can do so:
while True:
h = input("Informe sua altura: ")
if h.replace('.', '', 1).isdigit():
break
print('formato errado')
print('altura:', float(h))
Wouldn’t it be more interesting instead of
float(h)
and thenreturn h
, do everything in the same line?return float(h)
?– Leonel Sanches da Silva
Yes @Ciganomorrisonmendez, this actually reduces a line. Obgado
– Miguel
thanks Miguel , but I wanted to understand what is wrong with this expression while h != float : I didn’t want to use def for this , I just want it to compare and see if it’s float or not
– Adriano Souza
But you need to repeat the input if it’s not right @Adrianosouza?
– Miguel
yes miguel unfortunately needs
– Adriano Souza
Valeu ae miguel, but I can not use neither Try nor except, only if and Else decision commands and loop for and while, for now I will make it way using the range function with a height that goes from 1.2 to 500
– Adriano Souza
Yes miguel but when I type 1.4 it also does not recognize as float
– Adriano Souza
Just to complement the answer, because I consider the most complete:
h = (input("Informe sua altura: "))
. these parentheses are unnecessary can be writtenh = input("Informe sua altura: ")
.– fernandosavio
Ha of course obgado @fernandosavio , well seen I think I was putting the float function before and forgot to take. Obgado
– Miguel