If your inputs are "sometimes" ints and "sometimes" floats then just wrap each input in a float(). You could make something more complex, but why would you?

Answer from CambridgeCro on Stack Overflow
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ read-input-as-a-float.aspx
Read input as a float in Python
There is no such method, that can be used to take input as a float directly โ€“ but input string can be converted into a float by using float() function which accepts a string or a number and returns a float value.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-convert-string-to-float
How to Convert String to Float in Python: Complete Guide with Examples | DigitalOcean
July 10, 2025 - This is the standard Pythonic way to handle exceptions in Python. ... The try block contains the code that might fail (the โ€œriskyโ€ operation). The except block contains the code that runs only if an error occurs in the try block. This allows you to catch the ValueError and handle it gracefully instead of letting your program terminate. input_string = "not a number" value_float = 0.0 try: value_float = float(input_string) print("Conversion successful!") except ValueError: print(f"Could not convert '{input_string}'.") print(f"The final float value is: {value_float}")
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ converting python strings to floats
How to convert Python strings to floats - IONOS
January 2, 2025 - This can happen if the user enters a value that canโ€™t be converted into a float, like a letter. In this case, a message says that the input is invalid and the loop requests valid input again. If you want to convert a Python string to a float with NumPy, you can use the function numpy.float64().
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-convert-inputs-to-float-safely-421320
How to convert inputs to float safely | LabEx
Float conversion transforms various input types to floating-point numbers ... ## Simple direct conversion value = float("3.14") print(value) ## Output: 3.14 ## Handling integer conversion integer_value = float(42) print(integer_value) ## Output: 42.0 ยท def safe_float_convert(value): try: return float(value) except (ValueError, TypeError): return None ## Example usage print(safe_float_convert("3.14")) ## Output: 3.14 print(safe_float_convert("invalid")) ## Output: None
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ string-to-float-python
6 Ways to Convert String to Float in Python | FavTutor
August 31, 2021 - If any number is greater than this, you can indicate them by the string โ€˜infโ€™ in Python ... You can use the float() function to convert any data type into a floating-point number. This method only accepts one parameter.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_float.asp
Python float() Function
Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The float() function converts the specified value into a floating point number. ... If you want to use W3Schools services as an educational institution, team or ...
Find elsewhere
๐ŸŒ
Cherry Servers
cherryservers.com โ€บ home โ€บ blog โ€บ python โ€บ how to convert string to float in python (6 different ways)
How to Convert String to Float in Python (6 Different Ways) | Cherry Servers
July 25, 2024 - Letโ€™s see an example of the ast.literal_eval() function in action. import ast input_string = "33.3" float_number = ast.literal_eval(input_string) print(float_number) print(type(float_number))
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-string-to-float-in-python
Convert String to Float in Python - GeeksforGeeks
July 15, 2025 - float() function is an efficient way to convert a string into a floating-point number. As a built-in function, it returns a value of type float and works best with well-formatted numeric strings.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ float
Python float() - Convert To Floating Point | Vultr Docs
November 22, 2024 - This code converts the integer 5 into the floating-point number 5.0. Using float() in this manner ensures type conversion from integer to float. Start with a numeric string. Use the float() function to turn the string into a floating-point number.
๐ŸŒ
Medium
medium.com โ€บ @Doug-Creates โ€บ convert-string-to-float-or-int-in-python-0d93ce89d7b1
Convert String to Float or Int in Python | by Doug Creates | Medium
March 24, 2024 - Install: No installation is needed as parsing to float or int uses Python's built-in functions float() and int(), which are readily available in the standard library. Parsing a string to a float or int is the process of converting a string that represents a number into a floating-point or integer data type, respectively. def parse_string_to_number(input_string): try: # Attempt to convert the string to an int return int(input_string) except ValueError: # If int conversion fails, attempt to convert to float try: return float(input_string) except ValueError: # If both conversions fail, return an
Top answer
1 of 5
3

Well, you shouldn't convert to float immediately. Also, this is not how you do do..while loop in Python.

while True:
    data = input('Enter a number or ENTER to quit: ')
    if not data: break

    data = float(data)
    # ...

This way you don't have to duplicate code, or to prolong the life of data name unnecessarily.

2 of 5
2

By default, the data type of input is string. Pressing ENTER will return an empty string which cannot be casted into a float, since the string is empty and there is nothing to be casted, which is generating the error. Here are two solutions to handle this error.

Solution 1

Do not cast input into float directly, but cast it when assigning the value of input to any variable.

data=input("Enter a number or just ENTER to quit:")

Add the following statement before the loop to handle the condition when the user wants to exit without entering any number.

if not data: # If user wants to exit without entering any number
    Max = 0
    Min = 0
else:  # Cast the inputs by user to float
    Max = float(data)
    Min = float(data)

Lastly add the following line at the beginning inside the loop.

data = float(data)

Complete Code

count=0.0
Sum=0.0
average=0.0
data=input("Enter a number or just ENTER to quit:")
if not data:
    Max = 0
    Min = 0
else:
    Max = float(data)
    Min = float(data)

while data!='':
    data = float(data)
    count+=1 
    number=float(data)
    Sum+=number
    average=Sum/count

    if data<Min:
        Min=data
    if data>Max:
        Max=data
    data=input("Enter a number or just ENTER to quit:")

print(count,("numbers entered."))
print("Sum:",Sum)
print("Average:",average)
print("Min:",Min)
print("Max:",Max)

Solution 2

What you can also do is set a default value for empty inputs. Here since you are casting input into a float, you can set any number as the default value for empty inputs. However, I will not prefer this because if the user enters the same number as the default value, the loop will be terminated. In the following example I've only modified two lines of your code and set the default value for empty inputs as 0.

count=0.0
Sum=0.0
average=0.0
data=float(input("Enter a number or just ENTER to quit:") or 0)
Min=data
Max=data

while data!=0:
    count+=1
    number=float(data)
    Sum+=number
    average=Sum/count

    if data<Min:
        Min=data
    if data>Max:
        Max=data
    data=float(input("Enter a number or just ENTER to quit:") or 0)

print(count,("numbers entered."))
print("Sum:",Sum)
print("Average:",average)
print("Min:",Min)
print("Max:",Max)
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python string to float conversion
r/learnpython on Reddit: Python string to float conversion
July 29, 2024 -

Hi all, I have a string a = '1721244344.700249000', I want to convert it to a floating value.

Float() is returning only 2 places after decimal point. Like 1721244344.7

Is there a way I can convert the entire string to a floating point value and get all the decimal (upto 9 places after decimal point)?

I have to use python v2.7 for this.

Edit: I do not have problem in printing the all 9 decimal places but I need the floating value so that I can subtract another value so that I get the difference with accuracy upto 9 th decimal point.

๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ python float() function: key concepts [with examples]
Python float() Function: Key Concepts [With Examples]
November 27, 2024 - Learn how the float() function in Python converts a specified value into a floating point number. Syntax: float(value). A key function for numeric conversions.
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ how to take float input in python
How to take float input in Python - Java2Blog
February 23, 2021 - q = float(input(โ€œEnter an float: โ€œ)) ValueError: could not convert string to float: โ€˜NAโ€™ ยท Thatโ€™s all about How to take float input in Python.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-29076.html
converting user input to float troubles
The comments in my script says it all, but to summarize, sometimes when this code adds say '5.24 or 3.46' or any other non integer number to the calculation it causes the result to be something like ex. 56.789999999996 when it 'should' be only 2 de...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ float-in-python
float() in Python - GeeksforGeeks
September 18, 2025 - In Python, the float() function is used to convert numbers or numeric strings into floating-point numbers. A floating-point number is simply a number with a decimal point (for example, 3.14, -0.5, or 10.0). This function is especially useful ...