Home > OS >  Elifs conditions are not working in my program
Elifs conditions are not working in my program

Time:07-08

This is my code that takes a number of codons. Codons are a group of three nucleotides, each coding for an Amino Acid
codon_sequence=[]

print("Enter no. of codons you want")

n=int(input())

for i in range(n):

codon=str(input())

codon_sequence.append(codon)

print(codon_sequence)

for i in range(n):

if(codon_sequence[i]=="UUU" or "UUC" or "TTT" or "TTC"):

    print("Phe_")

elif(codon_sequence[i]=="UUA" or "UUG" or "CUU" or "CUC" or "CUG" or "CUA" or "TTA" or "TTG" or "CTT" or "CTC" or "CTG" or "CTA"):

    print("Leu_")

elif(codon_sequence[i]=="UCU" or "UCC" or "UCG" or "UCA" or "AGU" or "AGC" or "TCT" or "TCC" or "TCG" or "TCA" or "AGT" or "AGC"):

    print("Ser_")

elif(codon_sequence[i]=="UAU" or "UAC" or "TAT" or "TAC"):

    print("Tyr_")

elif(codon_sequence[i]=="UGU" or "UGC" or "TGT" or "TGC"):

    print("Cys_")

elif(codon_sequence[i]=="UGG" or "TGG"):

    print("Trp_")

elif(codon_sequence[i]=="CCU" or "CCC" or "CCA" or "CCG" or "CCT"):

    print("Pro_")

elif(codon_sequence[i]=="CGU" or "CGC" or "CGA" or "CGG" or "AGA" or "AGG" or "CGT"):

    print("Arg_")

elif(codon_sequence[i]=="CAU" or "CAC" or "CAT"):

    print("His_")

elif(codon_sequence[i]=="CAA" or "CAG"):

    print("Gln_")

elif(codon_sequence[i]=="AUU" or "AUC" or "AUA" or "ATT" or "ATC" or "ATA"):

    print("Ile_")

elif(codon_sequence[i]=="AUG"):

    print("Met_")

elif(codon_sequence[i]=="ACU" or "ACC" or "ACA" or "ACG" or "ACT"):

    print("Thr_")

elif(codon_sequence[i]=="GUU" or "GUC" or "GUA" or "GUG" or "GTT" or "GTC" or "GTA" or "GTG"):

    print("Val_")

elif(codon_sequence[i]=="GCU" or "GCC" or "GCA" or "GCG" or "GCT"):

    print("Ala_")

elif(codon_sequence[i]=="GGU" or "GGC" or "GGA" or "GGG" or "GGT"):

    print("Gly_")

elif(codon_sequence[i]=="GAU" or "GAC" or "GAT"):

    print("Asp_")

elif(codon_sequence[i]=="GAA" or "GAG"):

    print("Glu_")

elif(codon_sequence[i]=="AAU" or "AAC" or "AAT"):

    print("Asn_")

elif(codon_sequence[i]=="AAA" or "AAG"):

    print("Lys_")

else:

    print("Stop_")

This is however, giving me only 'Phe_' as result, and ignores all other conditions

CodePudding user response:

Reason why your code is not hitting the elif blocks

Your if and elif blocks should look like this.
It should check if codon_sequence[i] is equal to a string of interest.

if(codon_sequence[i]=="UUU" or codon_sequence[i]=="UUC" or codon_sequence[i]=="TTT" or codon_sequence[i]=="TTC"):

Instead you have an or condition against just plain strings like UUC.
This will result in the first if condition always being True.
Thereby you will never hit the elif block.

Also a better way of writing the if statement would be:

if codon_sequence[i] in ["UUU", "UUC", "TTT", "TTC"]:
    print("Phe_")

CodePudding user response:

This would be a great candidate for a switch statement, but as the previous answer mentioned you can't put an "or" between each string like you're doing.

  • Related