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 trialRussell Berry
Python Web Development Techdegree Student 1,640 Pointsint(booger * booger) vs int(booger) ** 2
So I got a cant multiply a non integer error for the first set of code, the second set worked fine.
Can someone briefly explain to me again why the top code doesn't work? Is it because the second set of code turns booger into an int before squaring it?
#MY CODE
def squared(booger):
try:
int(booger)
except:
return booger * len(booger)
else:
return int(booger * booger)
#=======================================
#=======================================
#GOOGLED CODE
def squared(booger):
try:
int(booger)
except:
return booger * len(booger)
else:
return int(booger) ** 2
1 Answer
Steven Parker
231,236 PointsIt looks like you answered your own question.
The answer is "because the second set of code turns booger into an int before squaring it". You can't multiply a string by another string.
Since you're trying to convert to integer in the try area, you could save the result and use it later instead of converting again:
def squared(booger):
try:
bogey = int(booger)
except:
return booger * len(booger)
else:
return bogey * bogey
Russell Berry
Python Web Development Techdegree Student 1,640 PointsRussell Berry
Python Web Development Techdegree Student 1,640 PointsRoger that! Thanks :)