import random
def pair():
base = random.choice('AGCT')
if base == 'A':
base = base + 'G'
elif base == 'G':
base = 'A' + base
elif base == 'C':
base = base + 'T'
else:
base = 'C' + base
return base
def sequence():
pair()
n = random.randint(1, 3)
print(base * n)
def main():
pair()
sequence()
pair()
sequence()
pair()
sequence()
main()
我必须在main函数中创建三个序列,将它们连接起来以形成更大的序列,然后打印结果。
但是我一直遇到错误,为什么?
追溯(最近一次通话):File "/Users/nicole/Desktop/CS/dna.py", line 33, in <module> main() File "/Users/nicole/Desktop/CS/dna.py", line 27, in main sequence() File "/Users/nicole/Desktop/CS/dna.py", line 23, in sequence print(base * n) NameError: name 'base' is not defined
答案 0 :(得分:0)
您需要为def sequence():
base = pair()
n = random.randint(1, 3)
print(base * n)
分配一个值。在您的情况下,看起来像这样:
ToString()
并且您应该尝试避免多次使用变量名,以便您可以更轻松地阅读代码。 :)