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

Why wont this work

def squared(argument):
    try:
        if argument == int(argument):
            return(argument * argument)
    except ValueError:
        return(argument * len(argument))
Anton Kamynin
Anton Kamynin
20,412 Points

Hi, Ryan. You do not need to use if statement, because 'try' and 'except' are conditional statements by themself. If 'try' works - good. If it`s not - moving to except.
Use 'try' to return square of argument, like this:

try:

return int(argument) ** 2

Anton Kamynin I figured it out but I'm just curious... this is what works

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

but doesn't the following make sense... with == instead of = because = is assigning?

argument == int(argument)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Using double-equals is a comparison that yields a True/False value. Regardless of the comparison results, argument is not altered. Then squared("5") would raise an error in the multiplication and the result would be "5" (the string times it length) instead of 25.

Using the single-equals assigns the converted value to the local variable argument that can be properly used in the multiplication.