-1 python if-statement startswith python-3.x ends-with
我是python的新手,我必须创建一个验证DNA序列的程序.(DNA序列的背景非常快)为了有效:•字符数可以被3整除•前3个字符是ATG•最后3个字符是TAA,TAG或TGA.
我的问题是在if语句中使用布尔项.
        endswith=(DNA.endswith("TAA"))
endswith2=(DNA.endswith("TAG"))
endswith3=(DNA.endswith("TGA"))
if length%3==0 and startswith==true and endswith==true or endswith2==true or endswith3==true:
    return ("true")
此代码返回以下错误:未定义全局名称"true"
我如何解决这个问题,而且在最后一个笔记中我真的很抱歉.这个问题的答案可能非常简单,在你的脑海中,一个2岁的孩子可以编码:/我做过研究,但我根本没有运气.所以我感谢你花时间去读我的愚蠢问题.
更容易:
return (len(DNA) % 3 == 0 and
        DNA.startswith("ATG") and
        DNA.endswith(("TAA", "TAG", "TGA")))
比较一个布尔值True或False几乎总是多余的,所以每当你发现自己这样做......做别的事情;-)
| 归档时间: | 
 | 
| 查看次数: | 10925 次 | 
| 最近记录: |