+ 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?
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.
+ 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')))
+ 3
Your string makes a float. I think it cannot be converted to an integer directly.
0
oh my god. That is so me. :-p
- 1
Paul Jacobs this is my mistake.
but ,print(int('34'+'3.4')) is not valid but why.