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 trialRyan Felton
1,471 Pointshow do I get squared to return the right answer
Im lost
def squared (tim):
return("timtimtim")
2 Answers
Eli Goodwin
29,502 Pointscurrently squared has a parameter 'tim'
def squared(tim):
but you do not do anything with the argument, you print out the string 'timtimtim'.
first, you need to check if input can be converted to either an integer or float.
second, if the input can be converted into an integer or float THEN you can square it.
third, if it cannot be converted into a number then you need to take the input and multiple it by its length.
the pseudo code would be:
function squared(input):
if type(float(input)) is float:
return input * input
else
return input * len(input)
Steven Parker
231,236 PointsThis return value would only be correct if the the function was given "tim" as the argument. But it needs to be able to adjust to different arguments, doing different things if the argument can be converted into a number.
For starters, the instructions give you a hint that "You'll probably want to use try
and except
..." And here's another hint: a typical solution will require about 5 lines of code.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsThis wouldn't be quite right since the argument might not be a number but could be converted to a number. That's why the instructions hint: "You'll probably want to use
try
andexcept
..."