Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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