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

I'm told this has a syntax error: return(num * len(num))

The challenge asks me to create a function that takes whatever argument (which I represent with 'num') and square it if it's a number, and if it's a string, multiply the string by the length of that string.

This is my latest iteration and I'm stumped. What am I doing wrong?

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) == True:
            return(num ** num)
        else:
            return(num * len(num))

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I see several issues with your code. The first being num ** num. If we passed in a four we'd get four to the fourth power... not four squared. You're also missing your except rule. Take a look at the code I have for this challenge:

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