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
🌐
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 ...
Discussions

How to make python accept both int and float input???
i need my calculator to accept both the inputs in form of integer and floating point number Neeeeed Hellp ASAP More on sololearn.com
🌐 sololearn.com
161
66
Int or float of an input function in Python - Stack Overflow
While taking an Intro to Python course through Plural Sight, I found that the code that worked for me was different than that of the instructor. (Different version?) There is one difference I do not More on stackoverflow.com
🌐 stackoverflow.com
How to accept only integer or float inputs?

I don't understand why you don't just use floats?

while True:
            try:
                time = float(raw_input("How much time has elapsed since your last stop (in hours)?"))
            except ValueError:
                print("Enter a valid number of hours!")
                continue

By typing an integer value, it'll just turn it into a float anyway.

More on reddit.com
🌐 r/learnpython
14
5
February 23, 2016
Float and int function
float() and int() are mainly use to convert strings to numbers. They actually create a number from the strings. int() is also often use to create an integer number from a float number. A classic example is when you ask the user for a number. The input function always returns a string of character. To perform calculation you need a number (int or float). So you must create a number from that string. As in: string_number = input("Enter temperature in °F (integer): ") temp_far = int(string_number) temp_cel = (temp_far-32) * (5/9) # the result here is a float print("Temperature in celsius (float)", temp_cel) temp_cel_int = int(temp_cel) # creates an integer number from the float print("Temperature in Celsius (integer)", temp_cel_int) Using float on strings from math import radians angle_string = input("Enter a precise angle in degrees with decimal: ") angle_float = float(angle_string) radians_float = radians(angle_float) print("That angle in degrees is",radians_float,"in radians") More on reddit.com
🌐 r/learnpython
10
5
March 25, 2023
🌐
IncludeHelp
includehelp.com › python › read-input-as-a-float.aspx
Read input as a float in Python
April 20, 2025 - Thus, we use input() function to read input and convert it into a float using float() function. ... # python code to take float input # reading a value, printing input and it's type val1 = input("Enter any number: ") print("value of val1: ", val1) print("type of val1: ", type(val1)) # reading ...
🌐
W3Schools
w3schools.com › python › ref_func_float.asp
Python float() Function
Python Examples Python Compiler ... Python Interview Q&A Python Training ... The float() function converts the specified value into a floating point number....
🌐
Medium
medium.com › @shaunak.edu › python-input-using-float-and-integer-dc37d2b74462
Python Input using float and integer - Shaunak - Medium
May 4, 2025 - In python the input () is used to take users input as a string. When working with integer or float you need to convert the input to the appropriate type using int() or float().
🌐
Java2Blog
java2blog.com › home › python › how to take float input in python
How to take float input in Python - Java2Blog
February 23, 2021 - In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed. ... Let’s see with the help of example. ... if you want to take float as input, then you need to use float() function to explicitly convert String to float.
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › float
Python Floats: Coding Essentials | Learn Now
When processing user input or data from external sources, you can use the float() function to convert a Python string to a float. Once converted, the numbers are safe to use for calculations. ... In some scenarios, you might need to convert floats to integers, either by truncation or rounding.
🌐
Javaexercise
javaexercise.com › python › take-float-input-in-python
Float Input In Python | Float() Method - Javaexercise
In Python, float() is a method that is used to convert user input into float type.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-input-float
How to take Float user input in Python | bobbyhadz
April 9, 2024 - 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. ... If the user enters an invalid float, we'd get a ValueError.
🌐
Emeritus
emeritus.org › home › blog › coding › what is a float in python and how does it benefit programmers
What is a Float in Python and How Does it Benefit Programmers
August 12, 2025 - However, if you want to run an operation on the input, you will have to convert the string object into a floating value. This is where the float() function plays an integral role.
🌐
CodeSpeedy
codespeedy.com › home › how to take float input in python
How to take float input in Python - CodeSpeedy
October 18, 2021 - In Python, It can’t identify the float() value directly as input.
🌐
LabEx
labex.io › tutorials › python-how-to-convert-inputs-to-float-safely-421320
How to convert inputs to float safely | LabEx
Float conversion is the process of transforming input data into floating-point numbers in Python.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-convert-string-to-float
How to Convert String to Float in Python: Complete Guide with Examples | DigitalOcean
Learn how to convert strings to floats in Python using float(). Includes syntax, examples, error handling tips, and real-world use cases for data parsing.
🌐
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.
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
TechBeamers
techbeamers.com › python-float-function
Python Float Function - TechBeamers
November 30, 2025 - Float() is a built-in Python function that converts a number or a string to a float value and returns the result. If it fails for any invalid input, then an appropriate exception occurs.
Top answer
1 of 4
1

input returns a string:

>>> x = input()
5.0
>>> x
'5.0'

If this string contains a representation of a number and you want to use it as a number, you need to call an appropriate function to parse the string and determine what number it represents:

>>> float(x)
5.0

However if the string doesn't represent a number, or if it doesn't represent a number of the type you're trying to convert it to, you will get an error:

>>> int(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.0'

(Technically 5.0 is an integer, but typically we think of '5.0' as representing not the number 5.0 itself but as representing some unknown number which would round to 5.0, i.e. some number between 4.95 and 5.05. Hence the failure to convert to an integer.)

And of course if the string doesn't represent a number at all, all of this is right out.

>>> float('banana')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'banana'

EDIT: 0x5453 raises a good point in the comments, which is that prior to python 3 the language would do these string-to-number conversions implicitly in some cases, which may be what you're remembering.

2 of 4
1

The function input() gets text from the user as a string, the float() function is used to convert strings to float values. In the inputs you specified any value containing a comma will cause float() to throw an error since float() does not know what to do with the comma. The function does know how to convert numbers containing decimals though so those numbers will be fine. If you wanted to take input that contains commas you would have to sanitize your input like so:

float(input("How much money do you own, in dollars?\n").replace(',','')) # 50,000 
🌐
Career Karma
careerkarma.com › blog › python › python float: a step-by-step guide
Python Float: A Step-By-Step Guide | Career Karma
December 1, 2023 - Because we are working with decimals, we’ll want our code to convert each number to a float. "Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!" Venus, Software Engineer at Rockbot Chat for Free with Your AI Career Advisor · Here’s the code we could use to create this game: answer = round(12.2 + 14.6, 1) student_input = input("What is 12.2 + 14.6?") if float(student_input) == answer: print("You're right!") else: print("The correct answer to 12.2 + 14.6 is", answer, ".")
🌐
Reddit
reddit.com › r/learnpython › how to accept only integer or float inputs?
r/learnpython on Reddit: How to accept only integer or float inputs?
February 23, 2016 -

My current code accepts integers that are greater than 0, which is what I want, but I also want to accept floats/decimals as well. How would I do this? Here is my current code:

while True:
            try:
                time = int(input("How much time has elapsed since your last stop (in hours)?"))
            except ValueError:
                print("Enter a valid number of hours!")
                continue
            if time > 0:
                givenTime[2] = time
                break

Indentation is off here for some reason, but believe me when I say that the indentation in the actual code is correct..

Can I just do time = int or float(input("How much time has elapsed since your last stop (in hours)?")) or would this be wrong? Any help would be appreciated.

edit: nope,

time = int or float(input("How much time has elapsed since your last stop (in hours)?"))

doesn't work.

🌐
Snakify
snakify.org › integer and float numbers
Integer and float numbers - Learn Python 3 - Snakify
When we read an integer value, we read a line with input() and then cast a string to integer using int(). When we read a floating-point number, we need to cast the string to float using float(): ... Floats with very big or very small absolute ...