Reputation: 75
I'm currently trying to make a very simply testing function that first checks to see if the users input is a multiple of 3, then tests each individual character to see if they are valid characters. Here's my code below:
def is_dna(string):
string.upper()
if(len(string) % 3 == 0):
print("LENGTH CORRECT")
for n in string:
if(n == "A" or n == "T" or n == "C" or n == "G"):
print("Valid")
else:
print("Invalid character")
break
return True
else:
print("Too many/little characters")
return False
When run, the bottom section will run fine, and if a correct amount of characters is used this will also successfully print the debug "LENGTH CORRECT" string. The issue is that the for loop will not initialize, and I haven't the foggiest why. Testing just the loop shows it to work fine; what is wrong with this function?
Upvotes: 2
Views: 86
Reputation: 1450
You don't need the cycle, just use regular expressions:
import re
def is_dna(string)
return re.match("([CAGT]{3})+", string.upper()) is not None
Upvotes: 0
Reputation: 56
simple fix you just need to have the string.upper() move into a variable then act upon the variable
code that is fixed
def is_dna(stri):
string = stri.upper()
if(len(string) % 3 == 0):
print("LENGTH CORRECT")
for n in string:
if(n == "A" or n == "T" or n == "C" or n == "G"):
print("Valid")
else:
print("Invalid character")
break
return True
else:
print("Too many/little characters")
return False
is_dna("ATCGCTATC") #this works and tests it perfectly
Upvotes: 2
Reputation: 149
string = string.upper()
the function upper() just return the uppercase of the character,the string self is not change.
Upvotes: 0