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

Joshua Senneff
Joshua Senneff
1,159 Points

I don't get what's wrong, using try and except to times a string by the number of letters in it

Please help, I don't get whats wrong

squared.py
def squared(num):
    try:
        int(num)
        num *= num
        return num
    except ValueError:
        list_num = list(num)
        num *= len(list_num)
        return num

1 Answer

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points
def squared(num):
    try:
        return int(num) * int(num)  # here you convert num to an int and return at the same time
    except ValueError:
        return num * len(num)  # if cannot convert then multiply num by the length of num
Joshua Senneff
Joshua Senneff
1,159 Points

Ah Ok, I think I get it now. You have to get the original num instead of a list of it, Thank you!