How to make a loop that compares string with a python float?

Asked

Viewed 447 times

7

I’m assigning the value 1.5 the variable h, but it does not accept values of the type float and does not complete the code.

h = (input("Informe sua altura: ")) # aqui ele recebe um elemento qualquer

while h != float:  # aqui faz a comparação(se fosse float deveria proceder)
    h = (input("Informe sua altura: "))  

    if h == float:
        print(h)

2 answers

6

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))
  • 1

    Wouldn’t it be more interesting instead of float(h) and then return h, do everything in the same line? return float(h)?

  • 1

    Yes @Ciganomorrisonmendez, this actually reduces a line. Obgado

  • 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

  • But you need to repeat the input if it’s not right @Adrianosouza?

  • yes miguel unfortunately needs

  • 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

  • Yes miguel but when I type 1.4 it also does not recognize as float

  • Just to complement the answer, because I consider the most complete: h = (input("Informe sua altura: ")). these parentheses are unnecessary can be written h = input("Informe sua altura: ").

  • Ha of course obgado @fernandosavio , well seen I think I was putting the float function before and forgot to take. Obgado

Show 4 more comments

3

Explicit conversion to float:

h = float(input("Informe sua altura: "))

See more here.

  • yes but if I do the conversion explicitly it gives me the error... that if I put any string value it is not a float value type enter my name it returns error so I did not put as float

  • What error? You can edit your question with it?

  • Valueerror: could not Convert string to float: 'dt' for example if I type a string

  • Ah, you want to treat anything that is read. For this case, @Miguel’s answer solves this perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.