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

what is answer?

what is answer?

squared.py
# EXAMPLES
# squared(5) would return 25
def squared(arg1):
    try: #try not Try
        intarg1 = int(arg1)
#        continue
    except:
        return arg1 * len(arg1)
        break
    return arg1 * arg1 # arg1 **2   
# squared("2") would return 4
# squared("tim") would return "timtimtim"

if arg1 is not integer and you want to do:

intarg1 = int(arg1)

this program will give ValueError, so needs for python:

except ValueError:

and you do not need break in this function because there is no loop

        except:
                return arg1 * len(arg1)
                break

and last mistake, if your arg1 is number but its data type string, you need to return square of int(arg1)

        except:
                return arg1 * len(arg1)
                break
        return intarg1 * intarg1

Note: pay attention for spaces before write statements

5 Answers

try:
    #your code
except ValueError:
    try:
        #another code
    except KeyError:
        try:

    #blah blah blah :)

if you learn more detail about try except, check other sites and see.

Ok, wanted to.. but i can only vote for my answer..will ask tech and upvote your comment.

ok, if you need help, ask again.

Thanks Ismail. Which are the "write statements" -pay attention for spaces before write statements?

Answer worked if except: as i am catching all errors not only ValueError.

try:
    #your code
except ValueError:
    try:
        #another code
    except KeyError:
        try:

    #blah blah blah :)

if you learn more detail about try except, check other sites and see (can you upvote my comment?)

Because spaces determine how your command will be independent for ex:

for i in range(0,15)                        #loop 1 -----> No spaces
    for j in range (1,5)                    #loop 2 -----> 4 spaces
        for t in range (0 ,8)               #loop 3 -----> 8 spaces
  • loop 3 in loop 2 (loop 3 not independent of loop 2)
  • loop2 in loop 1 (loop 2 not independent of loop 1)

Hi Ismail, tree tech support hs replied that you replied as comment and not as an answer. thus i cannot upvote.

no problem

you want to post again as answer? I can upvote you then.