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 trialIan perry
2,674 PointsJust feeling lost.
Just on this Squared challenge and feeling lost to what they are looking for tbh?
3 Answers
Tobias Helmrich
31,603 PointsHey Ian,
at first you have to define your function. Then you should create a try block where you change your argument to an integer with the int method. If this doesn't work for some reason, because it's a string for example, you want to catch the error and return the argument multiplied with the length of the argument which you get with the len method. If there's no error you can just return the square of the argument with num ** 2 or num * num.
This is a working example how you could do it:
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
try:
num = int(num)
except ValueError:
return num * len(num)
return num ** 2
I hope that helps, if not or you have further questions feel free to ask them, good luck! :)
Ian perry
2,674 PointsYou know when you just need to hear things in plain english.
Thanks Tobias :)
Melanie Villela
3,048 PointsHi Tobias,
I have a question. I know this challenge wants us to use "try" and "except" but couldn't you also do "if" and "else"? Unless I missed it the video did not really go over what is ValueError. Is ValueError only used with the try and except functions? Does ValueError only work to check whether the previous try function is a str or int, whichever is specified?