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! While you're at it, check out some resources Treehouse students have shared here.

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

Python Python Basics (2015) Number Game App Squared

Ashley Keeling
Ashley Keeling
11,476 Points

can someone help please I don't get the question

the question is: This challenge is similar to an earlier one. Remember, though, I want you to practice! You'll probably want to use try and except on this one. You might have to not use the else block, though. Write a function named squared that takes a single argument. If the argument can be converted into an integer, convert it and return the square of the number (num ** 2 or num * num). If the argument cannot be turned into an integer (maybe it's a string of non-numbers?), return the argument multiplied by its length. Look in the file for examples.

squared.py
def squared(int):
    try:
        if

# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

2 Answers

Manish Giri
Manish Giri
16,266 Points

The argument passed to your function, can be either a number, or a non-numberic type, like a string. If it is a number, you can convert it to an integer, and return the square of the integer. You'll want to do this in the try block.

If this fails in the try block, then in the corresponding except block, you'll multiply the argument by it's length.

Examples -

squared("2") - the string "2" can be converted to the integer 2, so you return it's square, ie: 4.

squared("tim") - the string "tim" cannot be converted to an integer, so you multiply "tim" by it's length, which is 3, and you get - "timtimtim". You return this as the result.

Ashley Keeling
Ashley Keeling
11,476 Points

thanks, could you show me the first part that I would do because I get it but I don't know how I would do it

thanks for the help

Manish Giri
Manish Giri
16,266 Points

Okay so in your try block, you want to try and convert the argument to an integer, that goes like this -

try:
    parsed_arg = int(num)
    # if this doesn't throw an exception, you'll just return the square of parsed_arg
catch ValueError:
    # since the conversion threw an error, you'll multiply the argument with it's length

Hope this helps!

Pepijn Dekker
PLUS
Pepijn Dekker
Courses Plus Student 5,231 Points

@Ashley: So my script compiles without issue but the challenge will not accept it. It's throwing "squared did not return the right answer". Probably a case of me having an extra something in the program that isn't needed. Did you make any further progress?

# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

def squared(num):
    try:
        if int(num):
            print(int(num) ** 2)
    except ValueError:
        print(num * len(num))

squared(5)
squared("2")
squared("tim")