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

This challenge is similar to an earlier one. Remember, though, I want you to practice! You'll probably want to use try a

Hello, I'm somewhat lost on what to do. Can someone please point me in the right directions.

Thank you!

def squared(num):
        for num in squared:

    if int(num) == True:
        return(num * num)
    except ValueError:
        return(num * len(num))

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

3 Answers

Hey christiano! Yes your a bit off here, i had trouble with this one the first time too tho! To break down the problems:

First you define a function named squared, that takes a single parameter "num". This is good.

Then you "try" to do a for loop for the squared function, and assigned the value of each iteration to the variable num? Im not sure what your trying to accomplish there, and you dont even put anything inside of that for loop, so basiclly it does nothing.

Then you have an if statement that runs if turning the num variable into an integer is the same as "True", if thats the case you return num * num. Im assuming what your trying to do here, is check if the variable num can be turned into an integer. But this is what you should use the try block for, because if have something that cant be turned into an integer and you run the int() on it, it will spit a ValueError. So try to move this statement to your try block and remove the "== True".

Finally your except ValueError is good.

Try to redo this, if you still need more help, feel free to write back. Happy coding!

Hello, thank you that makes a more sense. Right now I'm getting: TypeError: can't multiply sequence by non-int of type 'str'

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

I guess i'm not converting the length of num to multiple by itself, but I'm not sure what the proper way would be?

Thank you,

Hey c c! Your so close, the only issue is that you dont need an else statement for this, just ask it to try to return num * num imdidiately after you "try" to turn num into an int. If it cant do that it will just to the valueError, and ignore that statement, so it wont mess with the code. Infact you can convert it to an int and return it at the same time like this:

return int(num) ** 2

Note that the " arguement ** 2" is way of saying squared. Hopet this helps!

Ahh, Thank you so much for your help!