-3
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()

I have to create three sequences in the main function, concatenate them to form a larger sequence, and print the result.

But I keep getting an error, why?
Traceback (most recent call last):

  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
stovfl
  • 14,998
  • 7
  • 24
  • 51
Nicole
  • 1
  • 1

1 Answers1

0

You need to assign a value to base. In your case it would look something like that:

def sequence():
    base = pair()
    n = random.randint(1, 3)
    print(base * n)

And you should try to avoid using a variable name multiple times, so you can read your code more easily. :)

Dennis Roth
  • 85
  • 1
  • 7