python - 最终输出的问题
问题描述
我正在研究获取基本元胞自动机代码的功能。我得到了正确的所有输出,但我无法打印出我想要的输出。
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 时绘制 #。
解决方案
推荐阅读
- asp.net-core - 使用 .NET Core 的中间件“健康检查”是否比控制器路由中的 ping 更有优势?
- javascript - JavaScript/React 按可变 JSON 键排序
- java - 如何强制浮点数以两位小数 (.00) 存储
- amazon-web-services - 访问 CloudFront Amazon S3 静态网站中的页面时访问被拒绝
- c# - 如何在 IAsyncEnumerable 发射器函数中正确使用 NpgsqlTransaction?
- python - Anacaonda/Windows 10 - ModuleNotFoundError:没有名为“scipy”的模块
- c++ - 在 C++ 中获取运行时变量的内存消耗
- makefile - Makefile递归方法问题
- vba - 运行时错误 91,对象变量或未设置块变量,lastrow
- reactjs - 反应元素内的变化值