为什么if语句被绕过?

时间:2016-12-02 21:10:19

标签: python if-statement

我想找出为什么第二个字符串" peptide_seq"绕过第一个if声明?这三个字符串应该通过if语句并返回以下语句:

  

将字符串转换为大写。

     
      
  • 包含任何不是A C G T或U的字符的字符串应返回:"不是明确的核苷酸"。
  •   
  • 包含ACGU的字符串应返回" DNA"。
  •   
  • 包含ACGT的字符串应返回" RNA"。
  •   
def nuc_ac_check(string):
    input_string = string.upper()

    if ('d') in input_string:
        return "not an unambiguous nucleic acid"
    elif ('A' and 'C' and 'G' and 'U') in input_string:
        return "Rna"
    elif ('A' and 'C' and 'G' and 'T') in input_string:
        return "Dna"

Rna_seq = "GGUACGGCUUGGUAUCCCACUCAGUGGCACCUGUGGCCU"
peptide_seq= "acgsdtushnsdses"
Dna_seq = "ggatacgatc"

print ('the rna seq variable is: ' + nuc_ac_check(Rna_seq))
print ('the peptide seq variable is: ' + nuc_ac_check(peptide_seq))
print ('the Dna seq variable is: ' + nuc_ac_check(Dna_seq))

5 个答案:

答案 0 :(得分:2)

第一个条件应该按照以下方式修复:

 if ('D') in input_string:
    return "not an unambigious nucleic acid"

注意大写' D'在这种情况下。

<强>更新 完整代码,使用Holloway和rassar评论: 更新1: 完整代码,使用Holloway和rassar,以及Copperfield评论:

def nuc_ac_check(string):
    input_string = string.upper()

    rna_letter = ['A', 'C', 'G', 'U']
    dna_letter = ['A', 'C', 'G', 'T']

    if ('D') in input_string:
        return "not an unambigious nucleic acid"
    elif all(letter in rna_letter for letter in input_string):
        return "Rna"
    elif all(letter in dna_letter for letter in input_string):
        return "Dna"
    return "Nothing of everything above"

Rna_seq = "GGUACGGCUUGGUAUCCCACUCAGUGGCACCUGUGGCCU"
peptide_seq= "acgsdtushnsdses"
Dna_seq = "ggatacgatc"
weird_string = "AGCTEXF"

print ('the rna seq variable is: ' + nuc_ac_check(Rna_seq))
print ('the peptide seq variable is: ' + nuc_ac_check(peptide_seq))
print ('the Dna seq variable is: ' + nuc_ac_check(Dna_seq))
print ('the weird string variable is: ' + nuc_ac_check(weird_string))

答案 1 :(得分:0)

你不能说:

elif ('A' and 'C' and 'G' and 'T') in input_string:

你必须说:

elif 'A' in input_string and 'C' in input_string and 'G' in input_string and 'T' in input_string:

或者你可以让它更简洁:

elif all(letter in input_string for letter in ('A', 'C', 'G', 'T')):

所以试试这个:

if all(letter in input_string for letter in ('A', 'C', 'G', 'U')):
    return "Rna"
elif all(letter in input_string for letter in ('A', 'C', 'G', 'T')):
    return "Dna"
else:
    return "not an unambiguous nucleic acid"

答案 2 :(得分:0)

.upper()将转换大写中的所有内容。其他条件如('A' and 'C' and 'G' and 'T')将始终给出一个char,因此您需要更新if子句。 你应该尝试这样的事情。

def containsAny(seq, aset):
    """ Check whether sequence seq contains ANY of the items in aset. """
    for c in seq:
        if c in aset: return True
    return False

def nuc_ac_check(string):
    input_string = string.upper() # update it as per your need

    if ('d') in input_string:
        return "not an unambigious nucleic acid"
    elif containsAny(input_string,['A','C','G','U']):
        return "Rna"
    elif containsAny(input_string,['A','C','G','T']):
        return "Dna"

Rna_seq = "GGUACGGCUUGGUAUCCCACUCAGUGGCACCUGUGGCCU" peptide_seq= "acgsdtushnsdses" Dna_seq = "ggatacgatc"

print ('the rna seq variable is: ' + nuc_ac_check(Rna_seq)) print ('the peptide seq variable is: ' + nuc_ac_check(peptide_seq)) print ('the Dna seq variable is: ' + nuc_ac_check(Dna_seq))

答案 3 :(得分:0)

我不确定你要做什么,但这可能会起作用:

dna_set = set("ATGC")
rna_set = set("AUGC")
nucleotide_set = set("ATGCU")

def nuc_ac_check(string):
    str_set = set(string.upper())

    if str_set - nucleotide_set != set():
        return "Not an unambiguous nucleic acid"
    if str_set == rna_set:
        return "RNA"
    if str_set == dna_set:
        return "DNA"
    return "Not DNA or RNA"

答案 4 :(得分:0)

只是询问字符串中是否包含"d""D"不足以排除无效字符串,因为它可能包含"E""Z",{ {1}},"X""1"或任何其他可能的角色,在我看来,最好的解决方案是使用set,这会减少你给它的任何东西不同的元素

例如

"@"

然后你只需要与有效选项的集合进行比较,看看是否有任何匹配

>>> set("111122222222ddddddddRRRRRRRDDDDD")
{'D', '1', 'd', '2', 'R'}
>>>  

,输出

RNA = set("ACGU")
DNA = set("ACGT")

def nuc_ac_check(string):
    d_r_na = set(string.upper())

    if d_r_na == DNA:
        return "DNA"
    elif d_r_na == RNA:
        return "RNA"
    else:
        return "not an unambiguous nucleotide"


Rna_seq = "GGUACGGCUUGGUAUCCCACUCAGUGGCACCUGUGGCCU"
peptide_seq= "acgsdtushnsdses"
Dna_seq = "ggatacgatc"

print('the rna seq variable is: ' + nuc_ac_check(Rna_seq))
print('the peptide seq variable is: ' + nuc_ac_check(peptide_seq))
print('the Dna seq variable is: ' + nuc_ac_check(Dna_seq))