Type conversion from string float value to int is failing

I am trying the below code but it is failing.

Var = int(‘3.3’)
Print(var)

Result:
Error is the result.

But i tried the following code

Var = int(‘3’)
Print(var)
Result:
3

I am using the following version.
Version: 2.7.7

'3.3' can not be translated into an integer, so int is failing. If you want to convert it into a float you can use float('3.3'). If you want to convert the float into an int you can use float(int('3.3')) to get 3. edit: as Cornelius points out, that was backwards, it’s int(float('3.3'))

(I’ll also note that you use Var for your assignment and try to print var, but maybe that was autocorrect)

This version of python has been unsupported since 2020, so I would definitely recommend updating to python 3. There is a small learning curve but since you are currently learning it shouldn’t be a problem, and this way you don’t learn anything that’s woefully obsolete.

5 Likes

Typo, correct is int(float('3.3')), the inner function get’s evaluated first.

1 Like