Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App Squared

Why won't this work?

def squared(num1):
    try:
        int(num1)
        return num1 * num1
    except:
        return num1 * len(num1)
  • >>> squared(2)
  • 4
  • >>> squared(5)
  • 25
  • >>> squared("tim")
  • 'timtimtim'
  • >>>

Look's like it works fine to me...What gives?

This is what frustrates me the most about TreeHouse's test environment. I waste a lot of time where my code works fine when I test it out but their test environment is so obtuse as it will only except one specific solution despite any other solutions that may also be working.

This problem is, I don't always know what "that" single solution is and end up wasting my time trying to to guess which the TreeHouse test environment wishes to see.

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

1 Answer

Forget it. I did another trial an error to make the test environment happy.

It was retarded as it wanted the following as a pass:

def squared(num1):
    try:
        num1 =  int(num1) * int(num1)
    except:
        return num1 * len(num1)
    return num1

Why it prefers it this way which is not the DRY method, who knows?...