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 trialKristian Woods
23,414 PointsDoes the return statement not allow for in-built function to be used within it?
I have two different solutions for this challenge, yet, the the second solution doesn't work. Why?
The only difference I made, was to put the arguments into their own variable. So I think it must be the return statement? Or something else I'm not picking up on
Thanks in advance
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
try:
number = int(num)
return n ** 2
except ValueError:
string_length = len(num)
return num * string_length
squared(4)
# top code works - BOTTOM code doesn't work
def squared(num):
try:
return int(num) ** 2
except ValueError:
return num * len(num)
squared(4)
1 Answer
Ryan S
27,276 PointsHi Kristian,
Your second solution works fine for me. I copied and pasted your code and it passed.
But your first solution doesn't pass because you are using an undefined variable 'n' in your first return statement.