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

please help me out.

I cant figure it out :(

squared.py
def squared(num):
    while int == num:      
        try:
            num * num

        except ValueError:
            return len(num) * len(num)
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

3 Answers

Samuel Ferree
Samuel Ferree
31,722 Points

'while' is a statement that creates a loop, which isn't what you're looking for here. You don't need that line of code at all. The except statement will catch anything that can't be multiplied by itself.

In your except block, you're multiplying the length of the string by the length of the string. In order to repeat a string, multiply the string itself by it's own length, like so:

string * len(string)

Thanks,but im still stuck and i dont know why. :( Any advice? :)

Samuel Ferree
Samuel Ferree
31,722 Points

You don't need to check the type of the num parameter. Just try to convert it, and let the except block handle any errors.

Here's my solution to the challenge

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

WoooW! I thought that i must use while, or if before the try. I see now, i can check if the num, is int inside return. Thank you very much :)