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

Please help with the task sequancer!

I dont know what is the problem. Because if I copy this to workspaces it works just fine, and it returns the correct number, or string. But here it gives me a typeerror that it cant multiply a string or something. Please help

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(var1):
    try:    
        int(var1)
        return (var1 * var1)
    except ValueError:
        a = len(var1)
        return (var1 + var1 + var1)

2 Answers

Hi! I have already solve the issue.

Just to add to this - your variable a isn't currently being used. Any input of type str will always return itself times 3, regardless of its length. Here's an alternative solution.

def squared(item):
    try:
        if isinstance(item, str):
            return item * len(item)
        else:
            return item * item
    except ValueError:
        print('Unexpected input type')