Welcome to the Treehouse Community
Looking to learn something new?
How do I solve it
I need to write a function named squared that takes a single argument.
If the argument can be converted into an integer, convert it and return the square of the number (num ** 2 or num * num).
If the argument cannot be turned into an integer, return the argument multiplied by its length. how can I convert a string to an integer
def squared(num): try int(num): return num*num except ValueError: return num*len(num) # EXAMPLES # squared(5) would return 25 # squared("2") would return 4 # squared("tim") would return "timtimtim"
Chris FreemanTreehouse Moderator 67,989 Points
You're very close.
int(num) will check if the conversion is possible but it doesn't retain the converted value. Either, update
num with assigned conversion:
try: int = int(num) return num * num except....
Or convert in the `return statement:
try: return int(num) * int(num) except....