姓名'转录'没有定义

时间:2015-02-09 06:44:39

标签: python function

def transcribe( s ):

    """ output: messenger RNA produced by string s
        input: a string s
    """
    if len(s)==0:
        return ''
    else:
        return one_dna_to_rna( s[0] )  + transcribe (s[1:])



 def one_dna_to_rna( c ):
        """ converts a single-character c from DNA
            nucleotide to complementary RNA nucleotide
        """
        if c == 'A': 
            return 'U'
        elif c == 'T': 
            return 'A'
        elif c == 'C': 
            return 'G'
        elif c == 'G': 
            return 'C'
        else:
            return ''

2 个答案:

答案 0 :(得分:1)

您的代码可以简化为以下三行:

dna2rna = { 'A':'U', 'T':'A', 'C':'G', 'G':'C' }
def transcribe2( s ):
    return ''.join(dna2rna[c] for c in s)

示例:

>>> transcribe2('ACT')
'UGA'

讨论

检查报告的错误消息:

  

第13行def one_dna_to_rna(c):^ IndentationError:unindent与任何外部缩进级别不匹配

这意味着def one_dna_to_rna( c ):的缩进有问题。

答案 1 :(得分:0)

def one_dna_to_rna相比,您似乎在def transcribe之前有一个额外的空格。这很可能是缩进错误的原因。