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!
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 trial
MING-HSIEN CHEN11,852 Points
def squared(num) not identified as good although it seems to work in my test why?
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)
billy mercier6,259 Points
You 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