Python: How would i write this 'if' statement for a word of arbitrary length?
Posted
by
ElCarlos
on Stack Overflow
See other posts from Stack Overflow
or by ElCarlos
Published on 2012-04-15T04:30:46Z
Indexed on
2012/04/15
5:30 UTC
Read the original article
Hit count: 473
python
|python-2.7
This is what I currently have:
wordlist = [fox, aced, definite, ace]
for word in wordlist:
a = len(word)
if (ord(word[a-(a-1)] - ord(word[(a-a)])) == ord(word[a-(a-2)])-ord(word[a-(a-1)]:
print "success", word
else:
print "fail", word
What I'm trying to do is calculate the ASCII values between each of the letters in the word. And check to see if the ord of the letters are increasing by the same value.
so for fox, it would check if the difference between the ord of 2nd and 1st letters are equal to the ord difference of the 3rd and 2nd letters.
However, with my current 'if' statement, only the first 3 letters of a word are compared. How can I rewrite this statement to cover every letter in a word of length greater than 3?
Sorry if I can't present this clearly, thanks for your time.
© Stack Overflow or respective owner