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

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

please can somebody excplain what i do wrong

It has to be checked if the argument is integer or not

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

def squared(num=''):

    try:
        if int(num) == num:

            return int(num)**2
        if int(num) != num:
            return num*len(num)
    except Exception:
          pass

1 Answer

Steven Parker
Steven Parker
231,236 Points

You're trying too hard.

This function won't take so many lines of code. Here's some hints:

  • you should only need to do an int() conversion one time
  • if no exception occurs, you can immediately return the square of the converted number
  • you won't need to compare the number with anything
  • if an exception does occur, return the string expanded by its length (don't "pass")
Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

This was the first thing what i was thinking but because it written "If the nr can be converted...or If can't..." i thought for the quiz i have to make it whit "if" statement. thank you