最终输出问题

2024-10-01 00:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

预期产量: Expected Output

我在做一些函数来得到基本元胞自动机的代码。我得到了所有的输出正确,但我不能得到我想要的输出打印

def main():
    N= int(input("Enter number of steps,N:"))
    C= int(input("Enter number of cells,C:"))
    i_list=list(map(int,input("Enter the indices of occupied cells (space-separated):").split()))
    cell= [0]*C
    for i in i_list:
        cell[int(i)] = 1
        displayCells(cell)
    for step in range(N):
        newCells = updateCells(cell)
        displayCells(newCells)
        cells = []
        cells += newCells      # concatenates new state to new list


def displayCells(Cells):
    for cell in Cells:
        if cell == 1:
            print("#", end='')
def updateCells(cells):
    nc = []          # makes a new empty list
    nc += cells      # copy current state into new list
    for a in range(1, len(nc)-1):
        if nc[a-1] == 1 and nc[a] == 1 and nc[a+1] == 1:
            nc[a] = 0
        elif nc[a-1] == 1 and nc[a] == 1 and nc[a+1] == 0:
            nc[a] = 1
        elif nc[a-1] == 1 and nc[a] == 0 and nc[a+1] == 1:
            nc[a] = 1
        elif nc[a-1] == 1 and nc[a] == 0 and nc[a+1] == 0:
            nc[a] = 0
        elif nc[a-1] == 0 and nc[a] == 1 and nc[a+1] == 1:
            nc[a] = 1
        elif nc[a-1] == 0 and nc[a] == 1 and nc[a+1] == 0:
            nc[a] = 1
        elif nc[a-1] == 0 and nc[a] == 0 and nc[a+1] == 1:
            nc[a] = 1
        else:
            nc[a] = 0
    return nc

main()

我希望输出是遵循updateCells函数规则的循环,并在程序看到1时绘制#


Tags: andofinnewforinputdefcell