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

Nico Pascher
Nico Pascher
5,857 Points

squared.py not working

Hi everyone,

can't find a correct answer to this task, even though I think I'm close. Can someone help finding the errors in my code?

Thank you so much.

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

def squared(arg):
    try:
        if int(arg) == True:
            int(arg) 
            return (arg * arg)
    except ValueError:
        return (arg * len(arg))

squared(5)

3 Answers

Juan Castro
Juan Castro
11,322 Points

The problem is that int(arg) doesn't return a boolean, it returns the integer value of arg. for example, int(5) just return 5, and int("hello") raise a ValueException. So when the arg variable is an int it does not pass the if statement.

You can omit the if statement since it's not necessary because the exception you need will raise in the int(arg) line.

So, your code should be:

def squared(arg):
    try:
        int(arg) 
        return (arg * arg)
    except ValueError:
        return (arg * len(arg))

Hey Juan,

Your code doesn't work.

Calling int(arg) doesn't change the value of arg, it only returns the integer version of arg.

Instead of:

int(arg)

You should write:

arg = int(arg)

This will achieve what the challenge wants.

Nico Pascher
Nico Pascher
5,857 Points

Thank you both so much. With your help I could solve the task and can now move on. Have a nice day!