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
John Michael
1,239 PointsPython Exception TypeError
Hello Everyone,
I am trying to implement an exception into my code that when there is a TypeError, which appears when trying to use and int, it will print ("this is not a string") I have tried using the except TypeError: return print ("this is not a string") but that doesn't work either.
def eCount (s):
count = 0
for index in s:
if index == 'e':
count = count + 1
elif index == 'E':
count = count + 1
# elif type(s) != str:
# print("This is not a nummber")
return count
except TypeError as e:
return print("This is not a string")
print (eCount ('Every e please.')) # should return 5
print (eCount (""))
print (eCount (5)
Thank you for the help!
1 Answer
nikolaiorenstrakh
280 PointsYou return from eCount before getting to except return count , there is no try before except, except is out of eCount function body. You shouldn't print return , just print or return is enough
nikolaiorenstrakh
280 Pointsnikolaiorenstrakh
280 PointsYou return from eCount before getting to except
return count, there is notrybeforeexcept,exceptis out of eCount function body. You shouldn'tprint return, just print is enough