Python Python Basics (2015) Number Game App Squared

MING-HSIEN CHEN
MING-HSIEN CHEN
10,521 Points

def 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

squared.py
# 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)
MING-HSIEN CHEN
MING-HSIEN CHEN
10,521 Points

hi 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

1 Answer

billy mercier
billy mercier
6,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