Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!