Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Udacity CS101:Python中的基本细胞自动机_Python_Computer Science_Cellular Automata - Fatal编程技术网

Udacity CS101:Python中的基本细胞自动机

Udacity CS101:Python中的基本细胞自动机,python,computer-science,cellular-automata,Python,Computer Science,Cellular Automata,有人能告诉我我的代码有什么问题吗(似乎是带有“num”的那一行)。我得到的字符串索引超出了范围,但在几乎相同的代码块中,它似乎可以工作。如果有一些代码可以在python中查看,我也希望看到它的链接。谢谢 def cellular_automaton(s,p,n): p = bin(p+256)[3:] s=s.replace('x', '1').replace('.', '0') while n>0: N = len(s) r=''

有人能告诉我我的代码有什么问题吗(似乎是带有“num”的那一行)。我得到的字符串索引超出了范围,但在几乎相同的代码块中,它似乎可以工作。如果有一些代码可以在python中查看,我也希望看到它的链接。谢谢

def cellular_automaton(s,p,n):
    p = bin(p+256)[3:]
    s=s.replace('x', '1').replace('.', '0')
    while n>0:
        N = len(s)
        r=''
        for i in range(N):
            num = int(s[(i - 1) % N] + s[i] + s[(i + 1) % N], 2)
            r += p[-1 - num]
            s = r
        n-=1
    s=s.replace('x', '1').replace('.', '0')
    return s
抱歉给你添麻烦了

问题似乎来自于线的不当缩进

s=r

我希望这对某人有用!另外,我也希望看到改进此方法的建议。

您能否提供错误消息的完整回溯,并在代码中标记消息指示的错误发生的行。实际上,我想我刚刚发现了问题所在:s=r的缩进不正确。但是谢谢你!