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 trialChristopher Beck
Courses Plus Student 4,667 PointsWhat is wrong with this code. Looks find to me and runs in workspaces like it should.
Not sure why I'm getting "Bummer! TypeError: can't multiply sequence by non-int of type 'str'"
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(x):
try:
int(x)
return(x*x)
except ValueError:
return(x * len(x))
1 Answer
Alexander Davison
65,469 PointsYour code doesn't satisfy this condition:
squared("2") would return 4
Because you will be trying to multiply two strings together.
This is because when you call int(x)
, it doesn't actually change the value of x
. It only return
s the integer version of x
.
If you want to modify the variable, you have to do this:
x = int(x)