+ 2

Why this code is invalid?

print(int('034'+'56')) #valid code print(int('34'+'3.4')) #invalid code but why can you tell me?

23rd May 2018, 4:06 PM
Maninder $ingh
Maninder $ingh - avatar
5 Answers
+ 4
In Python you cannot convert a float that is written as a String directly to an integer. Here: int("34") : legal int(34.6) : legal int("34.6"): illegal float("34.6"): legal If you have a float as a String and you want it converted to an int. First convert the String to a float. Then convert the resulting float to an int.
23rd May 2018, 4:40 PM
cyk
cyk - avatar
+ 3
There is a "," in the second line. If you remove it the second number is a float. This line works: print(int(float('34'+'3.4')))
23rd May 2018, 4:12 PM
Paul
Paul - avatar
+ 3
Your string makes a float. I think it cannot be converted to an integer directly.
23rd May 2018, 4:21 PM
Paul
Paul - avatar
0
oh my god. That is so me. :-p
23rd May 2018, 4:18 PM
Masquerade
Masquerade - avatar
- 1
Paul Jacobs this is my mistake. but ,print(int('34'+'3.4')) is not valid but why.
23rd May 2018, 4:18 PM
Maninder $ingh
Maninder $ingh - avatar