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.

Bob Main
4,183 Pointssquared() function code challenge (no "else"?) [Solved]
The squared challenge mentions that you "might" not need the else block...
Here's my code
def squared(A_arg):
try:
num = int(A_arg)
except ValueError:
return(A_arg * len(A_arg))
else:
return(num ** 2)
I just can't see how I could get around not using the else block without an error. Would it makes since (or even work)?
[MOD: added ```python markdown formatting -cf]
1 Answer

Bob Main
4,183 PointsOhhhh! I think I figured it out.
def squared(A_arg):
try:
num = int(A_arg)
except ValueError:
return(A_arg * len(A_arg))
return(num **2)
I think return ends the fuction. So i just put return(num ** 2) where the else block was and it worked! Sorry for the lack of indentation on my code. It was indented when I posted question.
Chris Freeman
Treehouse Moderator 67,989 PointsChris Freeman
Treehouse Moderator 67,989 PointsGreat Job! It looks correct to me. Marking post as Solved.