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 trialgary loo
Python Web Development Techdegree Student 557 Pointswhat is answer?
what is answer?
# 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"
5 Answers
Ismail KOÇ
1,748 Pointstry:
#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.
gary loo
Python Web Development Techdegree Student 557 PointsOk, wanted to.. but i can only vote for my answer..will ask tech and upvote your comment.
Ismail KOÇ
1,748 Pointsok, if you need help, ask again.
gary loo
Python Web Development Techdegree Student 557 PointsThanks 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.
Ismail KOÇ
1,748 Pointstry:
#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?)
Ismail KOÇ
1,748 PointsBecause 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)
gary loo
Python Web Development Techdegree Student 557 PointsHi Ismail, tree tech support hs replied that you replied as comment and not as an answer. thus i cannot upvote.
Ismail KOÇ
1,748 Pointsno problem
gary loo
Python Web Development Techdegree Student 557 Pointsyou want to post again as answer? I can upvote you then.
Ismail KOÇ
1,748 PointsIsmail KOÇ
1,748 Pointsif 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
and last mistake, if your arg1 is number but its data type string, you need to return square of int(arg1)
Note: pay attention for spaces before write statements