Python Python Basics (2015) Number Game App Squared

Didier Borel
Didier Borel
2,837 Points

why is this function not working

can someone tell what is wrong here

squared.py
def squared(arguement):
    if type(arguement)==int:
        arguement**2
    else:
        len(arguement)*len(arguement)

3 Answers

Didier Borel
Didier Borel
2,837 Points

Behar, thanks for your quick response. I am tied up with something else at the moment, but I will look later. Thanks for you effort, and I will play around with your second answer and let you know

I can defintely see your idea here, but you should be using try an except, because the challenge actually wants you to square strings that can be turned into integers aswell.

So say you have squared("2") Should return 4.

Also squared(2) should return 4. So instead of checking for type, simply try to turn the aruguement into an integer, and if that can be done, square it, else multiple the length of the string by itself.

Didier Borel
Didier Borel
2,837 Points

hmm, what answer do you suggest, i don't follow?

Well i am not going to show the finished code, but i can give you some pointers. There is a function that 'trys' to to turn something into an int, it will spit a valueError if it cant, so look here.

try:
    int(arguement)
    # If it can do that it will go here
except ValueError:
    # If it cant it will go here.

So if we have:

test = "5"
test = int(test)
# Now test would be the integer 5.

Try to see if you can solve it with that, else feel free to write back!