Why am I getting a string out of range error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why am I getting a string out of range error?

l1 = [] for i in range(2, 300000): x = str(i) for j in range(len(x)): if sum(list(map(lambda m : int(m[j])**5, x))) == i: l1.append(i) print(l1) I dont see why I'm getting a string out of range error in this code. Sorry for the copy paste but I don't know how to add a code in a message.

9th Jul 2017, 1:20 PM
sam
2 Answers
+ 3
Hmm... you use m as an argument in lambda. So what is m[j] supposed to mean?
9th Jul 2017, 1:37 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
I use the lambda function on x, so m[j] is the 'value' of the string at index j. It's really strange because this loop works for i in range(10), but when i gets bigger than 10 it will give an error. when I use j in range(len(x)-1) it will work for i in range(10, 100). When I use j in range(len(x)-2) it will work for i in range(100, 1000) and so on. I really don't see why that happens.
9th Jul 2017, 1:43 PM
sam