[Tutor] Printing Complimentary strand of the DNA sequence:

Mats Wichmann mats at wichmann.us
Fri Oct 4 14:16:11 EDT 2019


On 10/4/19 4:39 AM, Mihir Kharate wrote:
> Hi!
> 
> I wrote this code to print out the complimentary strand of the DNA
> sequence that is prompted to input.
> 
> (In a DNA sequence, the base 'A' binds to the base 'T' and 'C' binds
> to 'G'. These base letters are compliments of each other). So the
> expected output for a 'ATTGC' should be 'TAACG'
> 
> The code I have written does the job, but instead of printing the code
> in a line, it prints every letter on a new line and it does this two
> times.
> 
> This is the pastebin site link to my code: (
> https://commie.io/#yCvdFhuj ). Please give me some insight on why it
> is doing this and how to fix this for expected output.

you really could have just pasted the code directly into your email:

     var_DNAseq = input("Insert your DNA sequence here: ")

     def Complimentary_Strand():
         for Compliment in var_DNAseq:
             Compliment = var_DNAseq.upper()
             for character in Compliment:
                 if character == "G":
                     print("C")
                 if character == "C":
                     print("G")
                 if character == "T":
                     print("A")
                 if character == "A":
                     print("T")

     print("Complimentary Strand of your sequence: ")

     Complimentary_Strand()


you get duplicates because you're already iterating over the string in 
the first for loop, then inside that you loop over it again - the outer 
loop should be omitted entirely.

print, if you don't tell it otherwise, outputs a line of text - 
including a newline.

you can use the "end" keyword to change that, as in:

print("C", end="")

or you can use some other technique here - sys.stdout.write('C') is much 
more literal and just puts out what it's told to; or you could collect 
the characters into a string and return it from the function, and then 
print it in the main program - that would feel more natural because you 
can use the returned string for other purposes than putting it out on 
the console.






More information about the Tutor mailing list