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

On which line do I go wrong?

I'm not sure if it's my syntax or if I've missed something, or added too much.

squared.py
def squared(num):
    while True:
        try:
            int(num ** 2)
            return num
        except:
            if int(num) == 0:
                return(num * len(num))

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

3 Answers

Steven Parker
Steven Parker
229,644 Points

Well, line 10 with "return squared(5)" isn't valid syntax nor is it related to the challenge instructions.

But also, the function should return one of two things, either the numeric value squared, or a string value replicated. It should never return the argument unmodified (as on line 5).

There's also no need for a loop (line 2), this function does just one thing (based on the input) and returns.

And why test for 0 (line 7)? If the value was 0 then it should not have caused an exception.

I thought that functions must be returned to ...function.

Steven Parker
Steven Parker
229,644 Points

Well, that last line isn't part of the function (it's not indented). Plus you would not want to call your own function inside your function, that creates an "infinite recursion loop".

Christian A. Castro
Christian A. Castro
30,501 Points

Hello Steven Parker , am I missing something in this code? Any good advice please?

def squared(x):
    try:
        if x == int(x):
            return (x * x)
    except ValueError:
        return (x * len(x))
Steven Parker
Steven Parker
229,644 Points

Your "if" test may fail without causing an exception, in which case the function doesn't return anything at all. Plus even it it works, you multiply the original argument instead of the converted number. But you don't really need to test anything, just return the result of converting the argument to integer and squaring it.

Also, it's best to create a new question for a different issue so you'll have a better chance of more/faster replies. Plus it's easier for people with similar issues to find later before they re-ask something already solved.

Steven Parker
Steven Parker
229,644 Points

Dan Holroyd — How's it going? Were you able to resolve your issue?

Well, I've gone back and tried to simplify it several times. It's not passing yet, and I'm not sure how to "test" for it to be an integer, but I'm hacking at it. This is what I have so far:

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

Also, how do I tag people in these?

Steven Parker
Steven Parker
229,644 Points

You're close, but you still need to convert "num" to int in the "try" block (you can do it right in the return statement).

The "try" does the job of "testing" if the conversion fails.

You can tag someone by typing an "@" character and then slowly typing their name. A box should appear with names and avatars, and when you get enough typed to make the name unique, click on the person's name or avatar in that pop-up box.