• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Python

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

teddythetwig

Member
Joined
Feb 4, 2009
Location
MD
Ok so this is the problem code:

def isWord(self, word):
if word == '':
return False
for x in word:
if x != "[a-zA-Z']":
return False
return True

I pass this function "TestWordThree" and on the first T it returns false. Any advice? The function is supposed to test it the given string contains only a-z
 

Cowboy Shane

Member
Joined
Jan 9, 2002
Location
Augusta, GA
I am pretty sure regular expressions don't work that way in Python. You are trying to match, say 'T', against "[a-zA-Z']" and T != [a-zA-Z]. The following works:
Code:
import re
def isWord(self, word):
    pattern = re.compile('[a-zA-Z]+')
    match = p.match(word)
    if m:
        return True
    else:
        return False
 
Last edited:

Cowboy Shane

Member
Joined
Jan 9, 2002
Location
Augusta, GA
I added the + into the regular expression to indicate I wanted at least one of those characters. This makes the check for '' redundant.