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

Mint Milano
Mint Milano
3,114 Points

could not get it right.

how to print string those many times?

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

def squared(arg):
    try:
        arg1=int(arg)
        return arg1*arg1
    except ValueError:
        return arg*len(arg)

2 Answers

Mint Milano
Mint Milano
3,114 Points

[SOLVED]i got the answer. discussion closed.

In addition to giving yourself a "Best Answer", can you also add the text [SOLVED] to the beginning of your question's title? Thank You!

Alonso Serrano
Alonso Serrano
15,341 Points

Oh. Too late.

A few tips, though:

You don't have to create that new variable and you can convert arg to an integer in the return statement line.

Also, you can complete the challenge without specifying the type of error.

def squared (arg):
    try:
        return (int(arg)**2)
    except:
        return (arg * len(arg))
Mint Milano
Mint Milano
3,114 Points

thank you for the tips sir.it is very encouraging for beginners like me.