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 trialbill rde
127 Pointsneed help
dont get it
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(a):
try:
return int(a) * int(a)
except ValueError:
print("")
squared("2")
3 Answers
Dan Garrison
22,457 PointsYour statement in the try code block is fine, but you still need to handle the situation where you can't convert the argument into an integer in the except code block (You also don't need to include TypeError in the Except block). The instructions say that if you can't convert argument to an integer then you need to multiply the argument by the length of the argument. Remember the method for checking the length of something in python is len().
cbrown
2,437 PointsIn the except code block, try: print( a * len(a))
Dan Garrison
22,457 PointsClose. You don't need to print this. You need to return the value. Also you don't need a second try in the in the except code block. Your entire function should look something like this:
def squared(arg):
try:
#Attempt to multiply the int version of the provided argument by the int version of itself
except:
#if an exception is thrown in the try statement because the argument cannot be converted to an int
#run the code here to multiply the argument by the length of the argument.
cbrown
2,437 PointsYes, in the exception block the only line needed is either: print (a * len(a)) or return (a * len(a)) depending on whether or not the instructions require the value to be printed or returned.
I meant "try writing this line of code in the exception block", not add a "try" in the exception block.
My apologies for the confusion.