Last updated: Apr 9, 2024
Reading time·3 min
To take a float user input:
input()
function to take input from the user.try/except
statement to make sure the input value is a float.float()
class to convert the string to a float.# ✅ 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')
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 the input, converts it to a string and returns the result.
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.
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.
try: user_input = float(input('Enter a float: ')) print(user_input) except ValueError: print('Enter a valid float')
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.
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
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.
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.
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')
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.
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.
You can learn more about the related topics by checking out the following tutorials: