Message from Python discussions

December 2018

— Easy, it's a bot just telling you to use `


That's because your input is a string, '5.5' in the first example when you do: b = float(a) b becomes a float number, next when you do c = int(b), c becomes the integer part of b, that is 5, in the second case, a is still a string '5.5' and when you do c = int(a) it throws the error

— But i think a is always a str. b=float(a) b is a float

— And even if a is a float.the codes are still should be logically right. but computer tells me wrong which drive me crazy..

Message permanent page

— a is not a float, it remains being a string, because you read it from input

— You can't cast from string to integer if the string is not correctly formatted (and you can't assure it will), but you can cast from float to integer because Python knows how to handle numbers

Message permanent page

— A str cant be converted to int by int ()? oh thanks i dont know this. thanks a lot, cj.

— It can, if it is correctly formatted

— Look at it rookie

— In both cases a is a str

— In the first one, a is '5', it can be converted to int
in the second one, a is '5.5', it can't be converted to int, you have to cast it to float first

Message permanent page

— Rookie
You can think of it like this
when a = 5.5
You cannot simply say
B = int(a)
Because that would be like saying
B = int(5.5)
And int cannot take a float value thus this will throw you an error

Message permanent page