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 trial

Python Python Basics (Retired) Things That Count Exceptions

When 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

Hi 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.

Hi 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

I 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 the except: will be used.

Got it !! Thanks Steve :)

Sivasuryateja Raparla
Sivasuryateja Raparla
3,966 Points

i got the same question. the try catch block was very confusing. Kenneth missed explaining two important things when explaining this video.

  1. The number input is a string and
  2. 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