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

Anusha Singh
PLUS
Anusha Singh
Courses Plus Student 22,106 Points

Confusing, Please help

I'm confuse in this challenge question, can anyone help? The question is {"This challenge is similar to an earlier one. Remember, though, I want you to practice! You'll probably want to use try and except on this one. You might have to not use the else block, though. Write a function named squared that takes a single argument. If the argument can be converted into an integer, convert it and return the square of the number (num ** 2 or num * num). If the argument cannot be turned into an integer (maybe it's a string of non-numbers?), return the argument multiplied by its length. Look in the file for examples"} I don't know the mistake in my code if find the mistake, please answer

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
    try: 
        return int(num) ** 2
    except: 
        return len(num) ** 2

1 Answer

andren
andren
28,558 Points

Your code is close to correct, but there are two issue with your last line of code.

The challenge asks you to return the argument multiplied by it's length, in your code you do not actually multiply it by using the multiplication operator (*), you use (**) which is a power to operator. In addition to that you are trying to multiply the length of the argument by 2, instead of multiplying the length with the argument itself, which is what the challenge asked for.

Fixing those two issues produces this code:

def squared(num):
    try: 
        return int(num) ** 2
    except: 
        return len(num) * num

which will allow you to pass the challenge.

Anusha Singh
Anusha Singh
Courses Plus Student 22,106 Points

Thanks a lot, I was trying to figure this out from a long time