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 trialAlexandra Ciorra
Python Web Development Techdegree Student 796 PointsI'm not sure how to finish the second part? Is my first correct?
I am trying to figure this our but most of the time i need a boost to understand. Is my firs part correct? If not where did I go wrong? How do I finish the else?
Thanks!
def squared (num):
try:
num=(int(input(num*num)
else:
return len(num)
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
1 Answer
behar
10,799 PointsHey Alexandra! Your slightly off track here. I would suggest watching the previous video again because it dosent seem you have picked up on the try and except idea. However in the comments there are examples of what the function is expecting to have returned with different kinds of inputs. While your code is not finished it still has a couple of problems. First is that your trying to int(input(num)).. There should be no input what so ever in this function. What the challenge says is: It will provide you with an unknown string or number, you have chosen to store this number or string in the variable "num". Now it wants you to try to turn this number into an integer, if you can do that return the square of that integer, if not return the legnth of the string multiplied by the string. Here is the code:
def squared(num): #Here your saying that the string or number should be stored in the variable num
try: #Here your asking the computer to try to do the following
num = int(num) #Here were trying to turn the num variable into a string, and store it in the variable "num"
return num ** 2 #Here were returning the square of that number
except ValueError: #If the variable cannot be turned into a integer it will spit a ValueError, and will do what comes after this code
return len(num) * num #Here were telling it to return the length of the string, multiplied by the string
If the last part is confusing, basicly if we have the string "test" and say "test" * 3, we will get "testtesttest". And the len() function allows us to find the len of a string, and return that length as an integer. Hope this helps but please feel free to write back if you need more help!
Alexandra Ciorra
Python Web Development Techdegree Student 796 PointsAlexandra Ciorra
Python Web Development Techdegree Student 796 PointsThank you so much for taking the time to explain. I pick up things along the way that I may have missed. For example any time I've seen int() used it's with input. I didn't know int would be used with just an argument. It's nice to have this avenue for learning as well because the videos don't quite explain everything. I understand now but I am going to take your advice and watch the video again.
behar
10,799 Pointsbehar
10,799 PointsHey again Alexandra! Great attitude, and yea the videos can be a little quick sometimes. You can mark this question as "solved" by sellecting a "best answer".