Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App Squared

Where am I going wrong with the code ?

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.

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

def squared(a):

    try:
        a = int()

        return(a * a)
    except ValueError:
        a = str()
        #print("It's a string")

        return(a * len(a))



squared('a')

2 Answers

Samuel Ferree
Samuel Ferree
31,721 Points

you need to pass a into the int and str functions, like so

a = int(a)
a = str(a)

Is it because we are using an argument when we define our function, we should use

a = int(a) a = str(a)

??

Samuel Ferree
Samuel Ferree
31,721 Points

int and str are functions that take variables as parameters, and they return those variables as integer and string types.

when using them to convert variables to different types, you always need to pass the variable into those functions.

Thank You Soooo Much.

def squared(a):

try:
    a = int(input("> "))

    print("{}".format(a * a))
except ValueError:

    print("It's a string")
    a = str(a)

    print(a * len(a)  )

squared('a')

Please help me out here--

If its an integer, it prints its square but if I enter any string, its not giving right output. I want to get o/p as following for any string I enter--

squared("tim") should return "timtimtim" ( argument multiplied by its length.)

Samuel Ferree
Samuel Ferree
31,721 Points

You don't need to input a, as it's passed in to your function squared.

def squared(a):
  try:
    #a = int(input("> ")) <- No need to do this.
    a = int(a) # <- This is what you want to do.
  #...

If you try to convert the input to an integer as soon as you get it, and it fails, you never successfully store the input value in a, so when you try to convert it to a string later, there is nothing in it.

Also, to add onto Samuel Ferree's post, you should't call the squared function.

The code challenge does this automatically for you :)

Good luck! :+1:

~Alex