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

Problems on code challenge: Squared.

Hey everyone!

So I've been fanageling my code since last night and for about an hour today with no luck. After reading other peoples problems on the forums and still coming to no conclusions I figured I'd ask for help :D

So, what am I doing wrong?

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

# remember to use try
# you also might want to use except

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

2 Answers

Ian, you need to try the conversion to an int, and return the square only if that works.

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

P.S., also note that except should line up with try

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey Ian,

you have to try to convert the argument to an integer using "int(num)" before doing anything else. Also "if ValueError:" has to be "except ValueError". You also want to use "return num * len(num)" when catching the ValueError exception.

Regards, Florian