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

Aiden Colley
Aiden Colley
926 Points

I'm not sure where to start, need help...

I have an idea on what i need to do, I just can't seem to implement it into the correct syntax. the question asks to convert anything that can be converted to a integer to be converted, then squared otherwise square the string. This is what I have so far. You can see I really have no idea on what to do:)

squared.py
def squared(yay):
    if yay == int:
        return yay*yay

1 Answer

Gabrielle Lamarche
Gabrielle Lamarche
8,587 Points

Hi Aiden,

There are a couple of strategies you could use for this example. "isinstance" is preferred in cases where you may be including inheritance as it will also check subclasses.

if isinstance(yay, int):
    return yay*yay

if type(yay) is int:
    return yay*yay

Here's a comparison I found quite helpful on StackOverflow:

isinstance(Vehicle(), Vehicle)  # returns True
type(Vehicle()) == Vehicle      # returns True
isinstance(Truck(), Vehicle)    # returns True
type(Truck()) == Vehicle        # returns False, and this probably won't be what you want.

I hope this helps!