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 trialmoe sal
920 PointsNot sure what the error "squared" didn't return the right answer" implies. Help!
it seems like I have done everything the description asks for.
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(argument):
try:
if argument == int(argument):
int(argument)
return(argument * argument)
except ValueError:
return(argument * len(argument))
3 Answers
Gabbie Metheny
33,778 PointsThe problem is your if statement: you don't need one! Try basically takes care of that part for you. Python will try to convert your argument into an integer, and if it can't, then the except will run. So, remove the if statement, and make sure you're setting your argument to equal the integer version of the argument, and it should pass!
Wesley Trayer
13,812 PointsHey, Moe! The problem lies in the line:
int(argument)
The integer form of "argument" isn't stored in a variable. Your code works for numbers, or a string of letters, but it fails when it receives a string filled with numbers.
Hope this helps! :)
Oszkár Fehér
Treehouse Project ReviewerHi Moe Your code it's ok just it doesn't catch one more case
# squared("2") would return 4
a string which is an integer for that it should be another if statement
if argument == str(int(argument)):
return int(argument) ** 2
Also the first return method, returns argument multiplied with himself instead of square
return argument ** 2
Happy coding