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 trialLevi Perlei
1,876 Pointsplease help me out.
I cant figure it out :(
def squared(num):
while int == num:
try:
num * num
except ValueError:
return len(num) * len(num)
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
3 Answers
Samuel Ferree
31,722 Points'while' is a statement that creates a loop, which isn't what you're looking for here. You don't need that line of code at all. The except statement will catch anything that can't be multiplied by itself.
In your except block, you're multiplying the length of the string by the length of the string. In order to repeat a string, multiply the string itself by it's own length, like so:
string * len(string)
Levi Perlei
1,876 PointsThanks,but im still stuck and i dont know why. :( Any advice? :)
Samuel Ferree
31,722 PointsYou don't need to check the type of the num parameter. Just try to convert it, and let the except block handle any errors.
Here's my solution to the challenge
def squared(num):
try:
return int(num) * int(num);
except ValueError:
return num * len(num);
Levi Perlei
1,876 PointsWoooW! I thought that i must use while, or if before the try. I see now, i can check if the num, is int inside return. Thank you very much :)