Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFaraz Ali
1,192 PointsWhen will the except clause get executed ? Because if we give a float , it will get converted to int in the try clause
When I give a float in the first place , wouldn't it always get converted to an int so that the except clause never gets executed ?? Need a little help here .
Thanks in advance
3 Answers
Steve Hunter
57,712 PointsHi there,
It won't be converted to an int - if there's a decimal place, the conversion to an int will fail; "blows up", I think the teachers called it, meaning that execution will move to the except:
Steve.
Faraz Ali
1,192 PointsGot it !! Thanks Steve :)
Sivasuryateja Raparla
3,966 Pointsi got the same question. the try catch block was very confusing. Kenneth missed explaining two important things when explaining this video.
- The number input is a string and
- In the shell he didn't walk through how the compiler would interpret the code when it is executed.
I am little disappointed in this video as kenneth left those two points
Faraz Ali
1,192 PointsFaraz Ali
1,192 PointsHi Steve,
Thanks for the reply.. But if we pass a decimal to an int function , say int(2.3) , wouldn't it return 2?? In that case try statement will always get executed , whether we provide an integer or a decimal number as an input .. Please help .. Thanks in advance
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI think you're passing a string into a method which will try to convert it to an int. I think that's a step too far. Yes, you can change floats to ints by casting them, but this is startng with a string - I think the first
try
will fail (try it!) so theexcept:
will be used.