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

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
How would I make a program only accept floats and integers?
The basic steps you need are: Ask for input Check if the input is a float If not a float, show an error message and return to step 1 Happy to help more with any of these specific steps, but try to figure them out yourself first (by googling). More on reddit.com
🌐 r/learnpython
10
8
October 21, 2022
Beginners Question Float
This is my script: num1 = input() operator = input() num2 = input() if operator == “-”: result = float(num1) - float(num2) elif operator == “+”: result = float(num1) + float(num2) elif operator == “*”: result = float(num1) * float(num2) elif operator == “/”: result = float(num1) ... More on discuss.python.org
🌐 discuss.python.org
11
0
August 3, 2021
What are blank float inputs defined as? Clearly not zero
When I put nothing into a float input, instead of it returning “num is 0” it returns as completely blank. (I made the code in 30 seconds to demonstrate the problem, and I am relatively new to coding.) num = float(input(… More on discuss.python.org
🌐 discuss.python.org
12
0
October 26, 2023
🌐
W3Schools
w3schools.com › python › ref_func_float.asp
Python float() Function
Python Examples Python Compiler ... Study Plan Python Interview Q&A Python Training ... The float() function converts the specified value into a floating point number....
🌐
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.
🌐
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 ...
Find elsewhere
🌐
IncludeHelp
includehelp.com › python › read-input-as-a-float.aspx
Read input as a float in Python
April 20, 2025 - Learn how to take float input in Python using the input() function, convert it to a float with float(), and handle decimal numbers efficiently.
🌐
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.
🌐
Python.org
discuss.python.org › python help
Beginners Question Float - Python Help - Discussions on Python.org
August 3, 2021 - This is my script: num1 = input() operator = input() num2 = input() if operator == “-”: result = float(num1) - float(num2) elif operator == “+”: result = float(num1) + float(num2) elif operator == “*”: result = float(num1) * float(num2) ...
🌐
Python
docs.python.org › 3 › builtins › functions.html
Built-in functions — Python 3.15.0 documentation
>>> float('+1.23') 1.23 >>> float(' -12345\n') -12345.0 >>> float('1e-003') 0.001 >>> float('+1E6') 1000000.0 >>> float('-Infinity') -inf · If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be '+' or '-'; a '+' sign has no effect on the value produced. The argument may also be a string representing a NaN (not-a-number), or positive or negative infinity. More precisely, the input must conform to the floatvalue production rule in the following grammar, after leading and trailing whitespace characters are removed:
🌐
Quora
quora.com › How-can-I-check-whether-or-not-a-user-input-in-Python-is-a-float-or-an-integer-without-using-try-error-checking
How to check whether or not a user input in Python is a float or an integer without using try error checking - Quora
Answer (1 of 9): I would try to answer a more fundamental question first: What is the canonical source of truth regarding what is and isn't a float or an integer in the context of what you are trying to do? This depends on the specification or requirements you are working with. Random strangers ...
🌐
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
🌐
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.
🌐
W3Schools
w3schools.com › python › python_user_input.asp
Python User Input
The input from the user is treated as a string. Even if, in the example above, you can input a number, the Python interpreter will still treat it as a string. You can convert the input into a number with the float() function:
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-receive-user-input-python
How to Receive User Input in Python: A Beginner’s Guide | DigitalOcean
February 13, 2025 - Learn how to receive user input in Python using the input() function, command-line arguments, and GUI methods. Explore examples, best practices, and common m…
🌐
Davrot
davrot.github.io › pytutorial › python_basics › input_output_string_int_float
input, print, string, int, float
Return a floating point number constructed from a number or string x. If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be ‘+’ or ‘-‘; a ‘+’ sign has no effect on the value ...
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Input command - Can you use float or integer values - Raspberry Pi Forums
January 30, 2019 - If you want to convert the string that input returns into an int OR a float you could use two try-except block to attempt to convert to int and float and catch the exceptions raised if either or both of the conversions fail. PeterO · Discoverer of the PI2 XENON DEATH FLASH! Interests: C,Python,P...
🌐
Python.org
discuss.python.org › python help
What are blank float inputs defined as? Clearly not zero - Python Help - Discussions on Python.org
October 26, 2023 - When I put nothing into a float input, instead of it returning “num is 0” it returns as completely blank. (I made the code in 30 seconds to demonstrate the problem, and I am relatively new to coding.) num = float(input(…
🌐
Emeritus
emeritus.org › home › blog › coding
What is a Float in Python and How Does it Benefit Programmers
August 12, 2025 - Float() parameters mean the name given to the float function in Python. For example, if you type float function: float(x) in the program, x is the variable or the parameter with a specific integer, string, or floating value.