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 trialHinh Phan
2,564 PointsSQUARED CHALLENGE. I don't get the challenge. Please help to explain.
def squared(ca):
if ca == 5:
return 25
def squared(ca):
if ca == 5:
return 25
4 Answers
Steven Parker
231,269 PointsLet me see if I can rephrase it to be clear. The function will take an argument, and then it will check the argument and do one of two things:
- if the argument is an integer, or something that can be converted into an integer (such as a string of digits), then convert it (if needed) and then square it. Return the square.
- if the argument can't be converted to a number (such as a string like "apple"), then "multiply" (repeat, using the * operator) the argument by its own length and return that.
So for examples, (and you will not code for these specifically), if you put in 3, you get back 9 (case 1).
If you put in "12", you get back 144 (case 1).
If you put in "Yes", you get back "YesYesYes" (case 2).
And if you put in "b3ast", you get back "b3astb3astb3astb3astb3ast" (case 2).
Does that help?
Hinh Phan
2,564 PointsHere is how I did. Still dont get it.
def squared(a):
a = "Got"
try:
a = int(a)
return a *a
except ValueError:
print("If the argument cannot be turned into an integer")
return "GotGotGot"
Steven Parker
231,269 PointsWell, you're kind of close there, but you need to use the argument that is passed in and not replace it with a literal string. Once you get rid of that assignment you'll have case 1 covered.
Then for case 2, you don't need to print anything. And instead of returning a fixed string, you'll need to multiply the argument by it's own length and return that.
Get those fixed and you're there.
Hinh Phan
2,564 PointsI got it. I'm so appreciated for your time. If possible, Can you give me some tip to solve the problems every time it occur and learn from them!
Steven Parker
231,269 PointsI can't think of any one tip that would help solve any problem that might occur. But the more you learn, the more you will be able to resolve issues yourself. And you can always ask questions on the forum if you get stuck.
Hinh Phan
2,564 PointsSound good, Tks Sir.