MING-HSIEN CHEN11,297 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