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 trialMING-HSIEN CHEN
11,852 Pointsdef squared(num) not identified as good although it seems to work in my test why?
Hello,
here are my lines of code
def squared(num): try: num1 = int(num) except ValueError: print (num * len(num)) else: print (num1 * num1)
when I put that into a python fiel and add squared (the examples give) they all return the right answer. I guess though it is not best as the text of the challenge said I might not have to use an "else"...
thanks for any help
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
try:
num1 = int(num)
except ValueError:
print (num * len(num))
else:
print (num1 * num1)
1 Answer
billy mercier
6,259 PointsYou need to return it.
def squared(num):
return int(num)**2
For your function to give out it's value you need to return something, and if you don't store that into a variable you lose it.
To store the value put it inside a variable.
a = squared(5)
print (a) will give 25
MING-HSIEN CHEN
11,852 PointsMING-HSIEN CHEN
11,852 Pointshi Again, Sorry I should have checked the other questions first. Well I've seen how others have done that. and I can actually directly try to return the int so that I do not use the else... thanks