Message from Python discussions
December 2018
— 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..
— 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
— 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
— 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
— Look at it rookie you can cast to float
in both cases
— Have you noticed the difference?