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

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?...