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 (2015) Number Game App Squared

squared() 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]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,427 Points

Great Job! It looks correct to me. Marking post as Solved.

1 Answer

Ohhhh! 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.