Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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
Python 定义函数时出错_Python - Fatal编程技术网

Python 定义函数时出错

Python 定义函数时出错,python,Python,我的任务是使o成为一个名为count_nucleotides的函数,它接受字符串、一个单词和一个字母,并计算字母的外观数。例如: 计算核苷酸('ATCGGC','G')并给出2 计算核苷酸('ATCTA','G')并给出0 我的代码如下: def count_nucleotides(dna, nucleotide): count = 0 num = nucleotide for char in dna: if char = num: count = count +1

我的任务是使o成为一个名为count_nucleotides的函数,它接受字符串、一个单词和一个字母,并计算字母的外观数。例如:

计算核苷酸('ATCGGC','G')并给出2

计算核苷酸('ATCTA','G')并给出0

我的代码如下:

def count_nucleotides(dna, nucleotide):

count = 0
num = nucleotide

for char in dna:
    if char = num:
        count = count +1
return count
它返回一条消息:预期为预期的块

我是python编程新手,所以如果有人能解释一下而不是问一些问题,比如在哪里定义这个或那个,我将不胜感激。这是我的第一个任务,请原谅我看到了什么

它返回一条消息:预期为预期的块

你必须这么做


是否检查了函数的缩进?函数的代码必须缩进到
def
语句下面。您的示例在
def count\u nucleotides()之后没有缩进行:
如果char=num:
这是一个赋值,您需要
==
来比较。如果char=num:是错误的,那么
也是如此。使用
==
进行比较。
def count_nucleotides(dna, nucleotide):
    count = 0
    num = nucleotide

    for char in dna:
        if char == num:
            count = count + 1
   return count