lst = ['a', 'b', 'c', 'd', 'e', 'i', 'u', 'y', 'l', 'i']
if(len(lst)%3==0):
pointer = 0
for i in lst:
for j in range(pointer, pointer+3):
print(lst[j])
pointer += 1
else:
rem = 3
pointer = 0
for i in lst:
for j in range(pointer, pointer+3):
print(lst[j])
pointer += 1
rem -= 1
if (pointer > rem):
pointer = lst [0]
print(lst[j])
I am not sure if I have a good approach or not, but I wanted my pointer to get to the first element of the list, if the number of elements is not divisible by 3.
When you say
pointer = lst[0]
the pointer variable will now be storing a string ( lst[0]
should be the string 'a'
from your list). So when your code later runs into the line:
pointer += 1
you are now attempting to add the string 'a'
to the integer 1
. That's where the type error comes from. You can either "add" two numbers, or "concatenate" two strings. You can't combine a string and an integer without explicitly converting one into the other.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.