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 trialTimothy Tseng
3,292 PointsHow come I can't iterate after coverting an int to str using str() (Solved)
Hi, how come when I try to run the below code I get a TypeError: 'int' object is not iterable
def maximum69Number (self, num: int) -> int:
string = str(num)
for digit in len(string):
if digit == '6':
digit = '9'
break
return int(string)
I realized using replace would be easier, but I'm just wondering why my for loop is causing a type error when I'm converting the number to a string.
Chris Freeman
Treehouse Moderator 68,423 PointsIt's not the immutability of strings that caused it to fail. len(string)
returns a single `int
value. This single value is assigned to digit
which subsequently will never be equivalent to a string value.
Timothy Tseng
3,292 PointsTimothy Tseng
3,292 PointsPlease disregard, I got the loop to work, but I realized that strings are immutable which is why the code is not working, I should have realized sooner as this is something that was covered early on in the Python track!