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

Chinmay Tare
Chinmay Tare
467 Points

Unable to solve this challenge. Please help :(

I'm unable to solve this problem.

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

3 Answers

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

First create the function named "squared". Second check if the received argument can be "parsed" to an integer inside of a try block. You could do it like this:

# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(value):
    try:
        return int(value) ** 2
    except:
        return value * len(value)

I hope that helps

Thank you Carlos your code works and got me to the next part. Can you please add a part in there for this part? >>

squared("2") would return 4 ---> i have tried to go through it on visulizer but i keep getting an error

Chinmay Tare
Chinmay Tare
467 Points

Thanks a lot Carlos!:D

Fenimore Cooper
Fenimore Cooper
3,591 Points

Hi Chinmay, I can't quite tell the context of the problem -- It looks like the problem is intended to get you to check the difference between inputs that can be converted to integers & those that are really strings. Checking the type with something like:

if type(n) == str:
     return n*3
etc..... ```

# won't work for the "2" input -- and a try / except block 
# would probably be the best way to go about it anyway.

``` python
def squareit(n):
     try:
          return int(n) ** 2
     except:
          return n*3  ```

# so the program attempts to convert the input into an integer
# if it fails, it will give an error & get caught by the exception 'block' 
# if you wanted you could be more specific you could use 'except ValueError:'
Parzeval One
Parzeval One
1,920 Points

This works in Workspaces when using "print" and not "return"

num = 5
def squared(num):
    try:
        print(int(num) ** 2)  
    except ValueError:
        print(str(num) * 3)
squared(num)

But how do I write it with "returns" instead? (I tried many versions)

num = 5
def squared(num):
    try:
        return int(num) ** 2
   except ValueError:
       return str(num) * 3
squared(num)

This is what i have managed to get from your code. Please try this:

def squared(num): try: return int(num) ** 2 except ValueError: return str(num) * 3

print (squared(5))

Does this help? with return it only goes to the function. You need a print statement to print out your return