samedi 16 mai 2020

"if" statement misbehaving - why is return value wrong?

The if statement miss behave at this stage. The word before the last one 'otuzz' is suppose to return 'to' not '(bo or o)'. I find it's quite weird for it works fine when moving it to the first word of the sentence. Somehow, it misbehave and bypass the if statement. May I know how to fix it without changing my test example, please?

def englishize_sentence(sentence):
    """return the Bee Latin version of the sentence."""
    latin = sentence.lower().split()
    for word_num in range(len(latin)):
        if ((latin[word_num][-4] in "b") and (latin[word_num][0] in "aeiou")) or (sentence[word_num][0].isalpha() == False):
            latin[word_num] = latin[word_num][:-4]
            latin[word_num] = "({} or {})".format(
                'b' + latin[word_num], latin[word_num])            
        else:
            latin[word_num] = latin[word_num][-4] + latin[word_num][0:-4]
    latin = ' '.join(latin)
    return latin

sentence = "onuzz hankyoutuzz ibuzz ambuzz allergicbuzz otuzz eggsbuzz"
english = englishize_sentence(sentence)
print(english)
no thankyou (bi or i) (bam or am) (ballergic or allergic) to (beggs or eggs) #The expected result

Aucun commentaire:

Enregistrer un commentaire