I have a function def next_generation(line, rule): where line is a list of booleans and rule is an integer
When you call it, it would look something like this:
>>> next_generation([True, True, True, False], 110)
[True, False, True, True]
The code takes the one before the first value (line[-1]), the first value (line[0], and the one after the first value (line[1]. Then does the same thing with the second value and so on. If that doesn't make sense here is a manual code of it that I wrote:
def next_generation(line, rule):
new_gen = []
first_cell = next_cell([line[-1], line[0], line[1]], rule)
new_gen.append(first_cell)
second_cell = next_cell([line[0], line[1], line[2]], rule)
new_gen.append(second_cell)
third_cell = next_cell([line[1], line[2], line[3]], rule)
new_gen.append(third_cell)
fourth_cell = next_cell([line[2], line[3], line[0]], rule)
new_gen.append(fourth_cell)
return new_gen
The code above works but only for a list of 4 boolean values. I want the code to do the same thing but loop through the list so it can do it for a list of any length.
Thanks in advance!
Edit:
I have now written:
def next_generation(line, rule):
new_gen = []
first_cell = next_cell([line[-1], line[0], line[1]], rule)
new_gen.append(first_cell)
for i in range(len(line)):
new_gen.append(next_cell([line[i], line[i+1], line[i+2]], rule))
last_cell = next_cell([line[-2], line[-1], line[0]], rule)
new_gen.append(last_cell)
But it is giving me the error IndexError: list index out of range. I'm pretty sure this is happening when it says
new_gen.append(next_cell([line[i], line[i+1], line[i+2]], rule))
How should I prevent this?
for i in range(len(line)):and a%and let us know if you get stuck!next_celldeclared?