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

Davies Oludare
Davies Oludare
4,900 Points

try, except returning error: squared didn't return the right answer

Please don't fix it, hints are fine :)

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"


def squared(a):
    try:
        return int(a) * a
    except ValueError:
        return len(a) * a

squared("aaa")

3 Answers

Davies Oludare
Davies Oludare
4,900 Points

I have fixed it now. Apologies

def squared(a): try: return int(a) * int(a) except ValueError: return a * len(a)

squared("aaa")

andren
andren
28,558 Points

It's a bit hard to just give a hint since your solution is pretty close. What I'll say is that the challenge expects the return of the function to be an int if a number was provided. Currently your function does not adhere to that expectation.

Edit: Well it seems my response was a bit too late to be helpful, but I'm glad you figured it out on your own. That's one of the best ways to learn when you start out with coding.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Davies! Wow, you're close! So yes, you are just going to get a hint :smiley:

Inside your try you are trying to convert a to an integer one time... but not both times. It needs to fail if either one of those fail.

Give this another shot with that hint in mind! :sparkles:

edited for additional information

Whoops! Seems like you figured it out while I was busy typing your hint. Kudos on figuring it out yourself! :sparkles: