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

What im i missing

Im sure i made multiple mistakes on this one, but not sure exacly what?

squared.py
def squared():
    if squared == int:
        return squared ** 2
    elif squared == str:
        return len(squared) ** 2

5 Answers

Steven Parker
Steven Parker
231,271 Points

It's the argument that you want to square or repeat, not the function name. So except for the first line, everywhere you have the word "squared" it should be "argument".

Then, in the "try" section, you need to perform a number conversion. So you might enclose the word "argument" in the "int" function ("int(argument)").

That final "if" line isn't proper syntax. But you don't need any code there anyway, since both the "try" and "except" sections both return a result.

got it, ty

Steven Parker
Steven Parker
231,271 Points

I strongly recommend you take the first hint given in the instructions: "You'll probably want to use try and except on this one." Also bear in mind that your function "takes a single argument".

If you don't resolve the rest of your issues while implementing those changes you can ask another question.

I feel dumb, bur i still dont get what im missing, here is my new code: def squared(): try: squared ** 2 except ValueError: len(squared) ** 2

Steven Parker
Steven Parker
231,271 Points

It's hard to read without code formatting, but it looks like you still haven't defined an argument for your function (it would go between the parentheses). It's the argument that you want to square or repeat, not the function name.

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

You also put

return len(squared) ** 2

This will return an integer instead of a string. You need to put

return squared * len(squared)

This will return a string. You also need to include try and except in the function. Along with that you need an arugument for the function e.g

def function_name(argument):
     return argument * len(argument)
def squared(argument):
    try:
        return squared ** 2
    except ValueError:
        return squared * len(squared)
    if squared == (str):
        return int(squared) ** 2

still cant figure this out. My new code looks like this. Im guessing i cant say if squared == (str):, but not sure how else to do it

Firstly you don't need that last if statement since you've already got a line that takes action when the argument is a string.

    except ValueError:
        return squared * len(squared)

Next you need to make sure that where you've put return squared you actually need to put:

return argument

This is because the argument is the number that you are squaring. for example if I was to use your function...

squared(5)

what I'm putting between the brackets is the argument for the function (what is going to be squared)

Apart from that the syntax of the code and the approach you've taken is perfect!