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 Functions and Looping Raising Exceptions

Youssef Moustahib
Youssef Moustahib
7,779 Points

(could not convert string to float: 'e')

My code is exactly like craigs, but when I type a letter instead of a number, I get this message:

What is the final sum? 20
What is the total number of people? e
oh no, thats not a valid value
(could not convert string to float: 'e')

I did not raise that exception or input that message?

1 Answer

Viraj Deshaval
Viraj Deshaval
4,874 Points

Hi Youssef, Strings cannot be converted to integer or floating point number because if you try float('e') Python will try to convert the 'e' but as its not a number it will raise an exception. But if you try float('3') it will be converted to floating point number because 3 is a number in a form of string.

Catch here is: Strings are only converted to floating point number if you try to convert numbers represented as string. Note: float('This is number 3') will not be converted because its a combination of letters and numbers strings and Python gets confused.

Hope I have cleared your doubt. Let me know if you face any challenges.