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 trialTrenton Cain
550 PointsI'm completely lost. I need assistance with finishing my code
import math
def squared(num): try: num ** 2 except TypeError: num * int(num) else: if num % 2 == 0: return num ** 2
import math
def squared(num):
try:
num ** 2
except TypeError:
num * int(num)
else:
if num % 2 == 0:
return num ** 2
2 Answers
Andrey Misikhin
16,529 PointsYou must save results in variables and use ValueError.
import math
def squared(num):
try:
num = int(num) ** 2
except ValueError:
num *= len(num)
return num
Steven Parker
231,269 PointsA few hints:
- the "int" conversion must be done inside the "try" block for it to detect exceptions
- the type of exception you will be looking for is "ValueError"
- you won't need an "if" test
- when the conversion fails, you need to multiply the argument by its length