Python Python Basics (2015) Number Game App Squared

I still don't get why this isn't working

Please help I so comfused

squared.py
def squared(num):
    if num == int:
    return int(num) ** 2
    else:
        return(num * num)
    except ValueError:
        return(num) * len(num)

1 Answer

Bryan Reed
Bryan Reed
11,013 Points

Instead of using an If and Else just use Try and Except. Part of why what you have isn't working is because you're using an Except without a Try

def squared(num):
    try:
        return int(num) ** 2 # OR int(num) * int(num). Either will give the SAME answer he's just giving you a choice
        # if the above return throws a ValueError because the num can't be converted into an integer
    except ValueError:
        return(num) * len(num)
        # then this except will run instead

Try and forget about using int(num) ** 2 or num * num because both of these operations will return the same answer