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 trialbehar
10,799 PointsWhat im i missing
Im sure i made multiple mistakes on this one, but not sure exacly what?
def squared():
if squared == int:
return squared ** 2
elif squared == str:
return len(squared) ** 2
5 Answers
Steven Parker
231,269 PointsIt'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.
Steven Parker
231,269 PointsI 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.
behar
10,799 PointsI 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
231,269 PointsIt'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. Or watch this video on code formatting.
Oli Flemmer
13,394 PointsYou 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)
behar
10,799 Pointsdef 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
Oli Flemmer
13,394 PointsFirstly 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!
behar
10,799 Pointsbehar
10,799 Pointsgot it, ty