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

Hinh Phan
Hinh Phan
2,564 Points

SQUARED CHALLENGE. I don't get the challenge. Please help to explain.

def squared(ca):
    if ca == 5:
        return 25
squared.py
def squared(ca):
    if ca == 5:
        return 25

4 Answers

Steven Parker
Steven Parker
231,268 Points

Let 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:

  1. 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.
  2. 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
Hinh Phan
2,564 Points

Here 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
Steven Parker
231,268 Points

Well, 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
Hinh Phan
2,564 Points

I 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
Steven Parker
231,268 Points

I 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
Hinh Phan
2,564 Points

Sound good, Tks Sir.