-2
s=input('what is your name\n')
l=int(input('how old are you?\n'))
print('you are',s,l,'years old')

When I assign 13 to l it works, but when I assign 13.5 I get:

l=int(input('how old are you?\n'))
ValueError: invalid literal for int() with base 10: '13.5'

Why is that?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
lilia
  • 3
  • 3
  • 1
    13.5 is a `float`, since it has decimal values. It would have shown the same error even if you assigned that value directly. You need to call `float()` rather than `int()` if you want to convert to float. – shriakhilc Jan 03 '22 at 21:04
  • 2
    Note that while `int("13.5")` fails, `int(13.5)` is all fine. Thus it’s possible (albeit somewhat hacky) to write `int(float(input( ... )))` – kirjosieppo Jan 03 '22 at 21:08
  • but why l=int(13.5) can be printed as 13, the inputted 13.5 above can not be printed? – lilia Jan 03 '22 at 21:13
  • 3
    `int()` can either parse a string into an integer, or convert a non-integer number to an intger (by removing the fraction). But it won't do both at the same time. If it's parsing a string, it has to be in the format of an integer. – Barmar Jan 03 '22 at 21:14
  • 2
    Because `13.5` and `'13.5'` are not the same value. `int` will happily convert the string representation of an integer to an `int`, but it will not try to guess what to do with a non-integer string just to get a value that *might* be convertible to an `int`. Better to just raise an exception to let the caller know they need to do some work of their own first. – chepner Jan 03 '22 at 21:15
  • 1
    *Any* time you try to call `int` on an unknown string, you should be using a `try` statement to catch the possible exception `int` might raise. You have no idea, in this case especially, what invalid string might have been passed as an argument. You can't assume that `int(input(...))` will work. – chepner Jan 03 '22 at 21:16
  • 1
    For your part, you can be clearer in telling the user what kind of input is *expected*. – chepner Jan 03 '22 at 21:17
  • Is there really not an appropriate duplicate for this? – Karl Knechtel Jan 03 '22 at 21:27

1 Answers1

1

It's not a float, it's a string, '13.5'. input() does not evaluate what you input. To be clear, int('13.5') is not valid.

If you want to allow float inputs, you'll need to handle them somehow, for example:

Use decimal instead

import decimal
age = decimal.Decimal(input(...))
>>> age = decimal.Decimal('13')
>>> age
Decimal('13')
>>> print(age)
13
>>> age = decimal.Decimal('13.5')
>>> age
Decimal('13.5')
>>> print(age)
13.5

Truncate

age = int(float(input(...)))
>>> int(float('13'))
13
>>> int(float('13.5'))
13

Round

age = round(float(input(...)))
>>> round(float('13'))
13
>>> round(float('13.5'))
14
>>> round(float('12.5'))  # Python uses banker's rounding
12

Use all floats

age = float(input(...))
>>> float('13')
13.0
>>> float('13.5')
13.5

In any case, you should use a try-except to catch invalid input, including invalid numbers like foobar and the exotic real numbers: inf, -inf, nan, and -0. See Asking the user for input until they give a valid response

wjandrea
  • 28,235
  • 9
  • 60
  • 81