首页 > 解决方案 > 最终输出的问题

问题描述

预期输出: 预期产出

我正在研究获取基本元胞自动机代码的功能。我得到了正确的所有输出,但我无法打印出我想要的输出。

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 时绘制 #。

标签: pythonlistfunction

解决方案


推荐阅读