我刚开始学习使用Python编程。在课堂上,我们被要求生成一个随机的DNA序列,该序列不包含特定的6个字母序列(AACGTT)。目标是创建一个函数,始终返回合法的序列。目前我的函数大约78%的时间可以生成正确的序列。如何让它100%返回合法序列?任何帮助都将不胜感激。
以下是我现在的代码:
以下是我现在的代码:
from random import choice
def generate_seq(length, enzyme):
list_dna = []
nucleotides = ["A", "C", "T", "G"]
i = 0
while i < 1000:
nucleotide = choice(nucleotides)
list_dna.append(nucleotide)
i = i + 1
dna = ''.join(str(nucleotide) for nucleotide in list_dna)
return(dna)
seq = generate_seq(1000, "AACGTT")
if len(seq) == 1000 and seq.count("AACGTT") == 0:
print(seq)