【发布时间】:2020-10-25 16:38:04
【问题描述】:
我的问题是:如何定义用户可以输入的输入代码,例如 3、5、6 或任何数字,以获得 3 个字母、5 个字母等的输出(根据用户输入)。
这段代码的输出是:
TC:2,
CG:2,
AT:3,
GC:1,
CA:1,
GA:1,
代码:
dna = "ATCGCATCGAT"
bases = ['A', 'C', 'G', 'T']
all_counts = {}
for base1 in bases:
for base2 in bases:
dinucleotide = base1 + base2
count = dna.count(dinucleotide)
if count > 0:
all_counts[dinucleotide] = count
for key, value in all_counts.items():
print(key, ':', value)
如果用户输入 3,则该代码将更改为:
数字 3 的输出是:
ATC : 2,
CAT : 1,
CGA : 1,
CGC : 1,
GAT : 1,
GCA : 1,
TCG : 2,
dna = "ATCGCATCGAT"
bases = ['A', 'C', 'G', 'T']
all_counts = {}
for base1 in bases:
for base2 in bases:
for base3 in bases:
dinucleotide = base1 + base2 + base3
count = dna.count(dinucleotide)
if count > 0:
all_counts[dinucleotide] = count
for key, value in all_counts.items():
print(key, ':', value)
所以,我的问题是:
我们如何通过用户输入来控制或添加新循环到这段代码?
例如,如果用户输入 4,程序会自动添加一个新循环,将字母分成 4 并输出如下:
ATCG : 2,
CATC : 1,
CGAT : 1,
CGCA : 1,
GCAT : 1,
TCGA : 1,
TCGC : 1,
【问题讨论】:
-
您好,您尝试
if声明了吗?在我写答案之前,我认为if是你想要的。对吗? -
你好,其实这段代码需要定义一个输入,我需要和用户交互。
-
你想用len n计算a(ny)子串的出现次数,其中n是用户输入的吗?
-
是的,确实,根据输入的数字,组成多少个字母组合。输出应该和例子一样。