My code is

def is_base_pair(str1, str2):
    if lens(str1) == 1 and lens(str2) == 1:
        if (str1 == "A" and str2 == "T") or (str1 == "G" and str2 == "C") or (str1 == "T" and str2 == "A") or (str1 == "C" and str2 == "G"):
            return True
        else:
            return False
    else:
        return False

What I want is to have both str1 and str2 to contain either one of these strings
"A" "T" "G" "C"

And combination of "A" "T" or "T" "A" and "C" "G" or "G" "C" with return True

And anything else will return False.


Anyone can help me here?

Edited 7 Years Ago by mms6: n/a

Whats lens()? Is that supposed to be len() ?

Here is one of several possible solutions to your problem:

>>> def ispair(strA, strB):
	# Is strA too long or short?
	if len(strA) != 1:
		return False

	# Is strB too long or short?
	if len(strB) != 1:
		return False

	# Are they a combination we are looking for ?
	if (strA, strB) in (('A', 'T'), ('G', 'C'), ('T', 'A'), ('C', 'G')):
		return True

	return False

>>> ispair('G', 'C')
True
>>> ispair('G', 'A')
False
>>> ispair('T', 'A')
True

Just a tip:
Using return ends a function right there, so you don't have to use so many if/else combinations if you do not want to.

>>> def test(x):
	if x == 1:
		return True
	print 'THIS SHOULD NEVER PRINT IF YOU ENTER 1!'

	
>>> test(10)
THIS SHOULD NEVER PRINT IF YOU ENTER 1!
>>> test(1)
True

Another tip:
You can make your code shorter, and easier to read by using iterators (list, tuples, sets, ect) for example:

>>> 1 in [1, 2, 3]
True
>>> 1 == 1 or 1 == 2 or 1 == 3
True

Same thing can be done with more values, you just need to put your iterator in sets:

>>> [1, 2] in [[1, 2], [3, 4], [5, 6]]
True
>>> [1, 2] == [1, 2] or [1, 2] == [3, 4] or [1, 2] == [5, 6]
True

Edited 7 Years Ago by lukerobi: n/a

I am a little lost. Would this be a correct statement?

str1 and str2 should contain one of A T G C
and form a combination of AT or TA and CG or GC

If that's the case then this should do ...

def is_base_pair(str1, str2):
    s = str1 + str2
    if s in ["AT", "TA", "CG", "GC"]:
        return True
    return False


# these should be true
print(is_base_pair('A', 'T'))
print(is_base_pair('T', 'A'))

print(is_base_pair('C', 'G'))
print(is_base_pair('G', 'C'))

print('-'*20)

# these should be false
print(is_base_pair('A', 'G'))
print(is_base_pair('C', 'T'))

Edited 7 Years Ago by vegaseat: n/a

This article has been dead for over six months. Start a new discussion instead.