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

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,722 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,722 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,722 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