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 trialBruce Röttgers
18,211 PointsQuestion is broken
This question is broken. I tried the code out in a workspace, works on all cases (examples: "5", "max"). But I get an error in the challenge.
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
try:
val = int(num)
print(val * val)
except:
length = len(num)
print(num * length)
else:
print("Not an Int or String")
2 Answers
andren
28,558 PointsThe challenge asks you to return
a number / string, not print
it. Returning and printing a value might look somewhat similar in the REPL for this type of code but they actually do very different things.
If you replace the print
statements with return
statements like this:
def squared(num):
try:
val = int(num)
return val * val
except:
length = len(num)
return num * length
else:
return "Not an Int or String"
Then your code will work.
AJ Salmon
5,675 PointsYou need to be returning the values, not printing them!
Bruce Röttgers
18,211 PointsBruce Röttgers
18,211 Pointsoh lol.