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

Benjamin Guyton
Benjamin Guyton
6,858 Points

Am I in the right ballpark?

I think len() applies here, but I'm not completely sure if I need to indicate that the input value will be a string. Any advice on what I'm doing wrong here?

squared.py
def squared(num):
    try:
        num = int(input("What number would you like squared? "))
    except ValueError:
        return(len(num) * len(num))
    else:
        return(num * num)

1 Answer

Rich Zimmerman
Rich Zimmerman
24,063 Points

You're close. You don't need to prompt, asking for the number you want to square since that value is being passed to the function as the parameter "num".

The purpose of the try/except block is to try and convert the 'num' to an int - which means "5" and 5.0 would convert to and int since they are actual numbers, where as if you passed "foo" as the parameter, when you try to convert it to an int, it would throw an error.

The challenge then wants you to print the value of 'num' times the length of the value of 'num'... so

print("foo" * len("foo"))
# would be
"foofoofoo" # foo * 3

Additionally, you don't need to use parenthesis in the return lines with python.

So you want your function to look something like this

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