samedi 2 avril 2016

Python IF statement keeps producing None between results [duplicate]

This question already has an answer here:

pig='ay'
Vowel= ('a','e','i','o','u')

WORDS= ('sphynxes', 'crypt', 'three', 'black', 'reached')
for word in WORDS:
    def trans(word):
        if len(word)>0 and word[0] in Vowel:
            print word+pig
        elif len(word)>1 and word[1] in Vowel:
            print word[1:]+word[:1]+pig
        elif len(word)>2 and word[2] in Vowel:
            print word[2:]+word[:2]+pig
        elif len(word)>3 and word[3] in Vowel:
            print word[3:]+word[:3]+pig
        elif len(word)>4 and word[4] in Vowel:
            print word[4:]+word[:4]+pig
        elif len(word)>5 and word[5] in Vowel:
            print word[5:]+word[:5]+pig
        elif len(word)>6 and word[6] in Vowel:
            print word[6:]+word[:6]+pig

for word in WORDS:
    print trans(word)

I want the result to be like this:

essphynxay
None
eethray
ackblay
eachedray

However, it produces:

essphynxay
None
None
eethray
None
ackblay
None
eachedray
None

I want to know why NONE is constantly printed in between. And I am looking for help to revise my code. Thank you.

Aucun commentaire:

Enregistrer un commentaire