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

Erik Luo
Erik Luo
3,810 Points

How to have both string and number as argument?

How to do this?

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


# A function with one argument
# if is integer, convert and return the square of the number
# if is not integer count the length and  multiplied by it self, then return
def squared(both):
    try:
        both = str or int
    except ValueError:
        return True
    except TypeError:
        return True
    if both = int:
        return (both * both)
    elif both = str:
        return (len(both) ** 2)

1 Answer

Don't overthink this.

def squared(both):
    try:  # The entire try/except isn't really doing much right now
        both = str or int  # This isn't going to do anything except assign both to str, meaning `both(3)` will return `"3"`.
    except ValueError:
        return True
    except TypeError:
        return True
    if both = int:  # Invalid syntax
        return (both * both)  # I don't think that you can multiply a class by itself
    elif both = str:  # Invalid syntax
        return (len(both) ** 2)  # Classes don't have lengths

This code will work for you.

def squared(arg):
    try:
        # If it's a number or can be converted to one, return the square of the number
        return int(arg) ** 2
    except ValueError:
        # Otherwise return the argument multiplied by it's length
        return arg * len(arg)
Erik Luo
Erik Luo
3,810 Points

It worked, but I have some questions. First, why won't "both = str or int" work? I thought it define both as either a string or an integer. Second, why is "if both = int:" and "if both = str:" Invalid syntax? And what's the correct way to do it? Third, why you don't have to define arg? Like "arg = "? Thank you.

Why won't "both = str or int" work?

str and int are Python objects. I'm not sure if you've gotten that far yet. Saying that a = b or c has Python look at both b and c. It then finds out if b is Truthy and c is Falsey or vice versa. In that case it assigns the Truthy value. If both b and c are Truthy or both are Falsey it will assign the first one. This doesn't work with class objects since they are Truthy. So it entirely ignores, in your case, int. but this only overwrites both as a str object. It doesn't confirm that both is a string.

Why is if both = int: and if both = str: invalid syntax?

Correct syntax is if both == int: and if both == str:. Note that the operand uses double == and not single. This still won't do what you want, you would want to use the built in function isinstance, as in if isinstance(both, int): and if isinstance(both, str):.

Why you don't have to define arg? Like arg =?

It's because arg is the argument that I passed into the function. Where you used both I used arg. Different name for the same thing.

Erik Luo
Erik Luo
3,810 Points

Thank you so much! I get it now!