0

Converting var to int

x = "2" int(x) print( x + 2 ) why does this input produce an error?

30th Dec 2016, 9:21 AM
Benedict Sun
Benedict Sun - avatar
4 Antworten
+ 9
x = "2" x = int(x) print( x + 2 ) ...see why...
30th Dec 2016, 9:34 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 2
You can also do print(int(x))
31st Dec 2016, 12:20 AM
Jess Wolfe
Jess Wolfe - avatar
+ 1
ah, that explains it, thanks!
30th Dec 2016, 9:39 AM
Benedict Sun
Benedict Sun - avatar
+ 1
this is because you have not assigned a variable for int(x) if you assign x="2" and then will print x+2 it will obviously show error you should define a variable y=int(x) then print y+2 it will show correct output
30th Dec 2016, 4:48 PM
Himanshu Vishwakarma