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 OverflowIf 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?
You could check for the presence of a decimal point in your string to decide if you want to coerce it into a float or an int.
number = input()
if '.' in number:
number = float(number)
else:
number = int(float(number))
How to make python accept both int and float input???
Int or float of an input function in Python - Stack Overflow
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!")
continueBy typing an integer value, it'll just turn it into a float anyway.
More on reddit.comFloat and int function
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.
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
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
breakIndentation 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.
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.
here's something I wrote a few months ago for a similar question. Figure out how to modify it to do what you want:
def input_type(msg, type_converter):
while True:
s = input(msg)
try :
return type_converter(s)
except ValueError as e:
print(e)
def input_lessthan(msg, maximum, type_converter=int):
while True:
n = input_type(msg, type_converter)
if n <= maximum:
return n
print("Please enter a number less than {}".format(maximum))
print(input_lessthan("hours worked> ", 168, float))