What is the best string to decimal code for floats i.e 0.5?

Hello everyone, I have not been able to workout how to convert a string to a float. I’ve tried a.isdecimal() , a.isdigit() and
a.isnumeric(). Please let me know if you have any advice.

I’m asking for an input from 0 - 1 (0.1 ,0.3, 0.67, etc) so no words or other numbers

You want to search for ‘Python string to float’. The float() function might be what you need.

This functions return if a str is digit or decimal, they don’t convert it
See, we can convert an input to integer using int(input('Enter a number'))
So you can use float in similar way…

I’ve got the input working now so thank you everyone for your help. Would you have any advice on how to ensure that the person can only input numbers and have strings rejected. I.e user: enters “Two” and is asked to input a number between 0 and 1. Thanks again

You can use a while loop…

while True:
     number = input("Enter a number")
     """ if number is an integer
         (you can use try, except or functions like isDecimal(), isDigit() etc.)
          number = int(number)

1 Like