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 trialNicholas Gaerlan
9,501 PointsMy answer seems correct
I tested block by rewriting it as a script that takes an input of either a string or number and then instead of return I just used print to see if it generates the expected outcome. Here's the code I used outside of the test:
def squared(num):
try:
print(int(num) ** 2)
except ValueError:
print(len(num) ** 2)
squared(num = input("square what? "))
That's not the answer I gave for the test of course. I just used it to see if my definition of the "squared" function would generate the expected result.
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
try:
return (int(num) ** 2)
except ValueError:
return (len(num) ** 2)
1 Answer
Steven Parker
231,236 PointsYou're halfway there.
If the argument is or converts to a number, you're good. But when an exception is thrown you don't want to output the square of the length, but instead the instructions asked you to "return the argument multiplied by its length".
Nicholas Gaerlan
9,501 PointsNicholas Gaerlan
9,501 Pointsthat must've been it. I simply had the length value "squared" as my exception result. I rewrote it to account for all the scenarios correctly. however, even though I found 2 different ways to do it, the answer wouldn't accept it. I just kept giving different ways to accomplish it until one of them finally worked.