How to take float user input in Python

avatar

Borislav Hadzhiev

Last updated: Aug 26, 2022

banner

Photo from Unsplash

Take float user input in Python #

To take a float user input:

  1. Use the input() function to take input from the user.
  2. Use a try/except statement to make sure the input value is a float.
  3. Use the float() class to convert the string to a float.
main.py
# ✅ Take user input float value user_input = float(input('Enter a float: ')) print(user_input) # ------------------------------------------------ # ✅ Take user input float value with validation try: user_input = float(input('Enter a float: ')) print(user_input) except ValueError: print('Enter a valid float')

take user input float

We used the input() function to take user input.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The function then reads the line from input, converts it to a string and returns the result.

Note that the input function is guaranteed to return a string, even if the user entered a floating-point number.

This is why we used the float() class to convert the value to a float.

main.py
user_input = float(input('Enter a float: ')) print(user_input)

If the user enters an invalid float, we'd get a ValueError.

You can handle the error using a try/except statement.

main.py
try: user_input = float(input('Enter a float: ')) print(user_input) except ValueError: print('Enter a valid float')

input float with validation

If the user enters an invalid float, the float() class throws a ValueError which gets handled in the except block.

You can use a while loop if you only want to allow the user to enter valid floating-point numbers.

main.py
num = 0 while True: try: num = float(input("Enter your favorite float: ")) except ValueError: print("Please enter a valid float") continue else: print(f'You entered: {num}') break

input only allow valid float

We used a while loop to only allow the user to enter a float value.

The code snippet keeps prompting the user for input until they enter a valid float.

If the code in the try block raises a ValueError, the except block runs, where we use the continue statement to continue to the next iteration.

If the user enters a valid float, the try block completes successfully and then the else block runs where we use the break statement to exit out of the while loop.

The continue statement continues with the next iteration of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

When validating user input in a while loop, we use the continue statement when the input is invalid, e.g. in an except block or an if statement.

If the input is valid, we use the break statement to exit out of the while loop.

You can use the same approach if you only want to accept float user input in a specific range.

main.py
num = 0 while True: try: num = float(input("Enter a float 1.1-9.9: ")) except ValueError: print("Please enter a valid float 1.1-9.9") continue if num >= 1.1 and num <= 9.9: print(f'You entered: {num}') break else: print('The float must be in the range 1.1-9.9')

user input float in range

We used a while loop to iterate until the provided input value is in a specified range.

If the try block completes successfully, then the user entered a float.

The if statement checks if the float is in the range 1.1-9.9 and if the condition is met, we break out of the while loop.

If the float is not in the specified range, the else block runs and prints a message.

If the user didn't enter a float, the except block runs, where we use the continue statement to prompt the user again.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.