jeudi 23 février 2017

Using While True loops

I am supposed to write a code that checks a word for certain syllables (examples: in, ex, are). If the word is made up of only those syllables, the code returns "Yes" and "No" otherwise (if the word also contains other syllables). The assignment requires me to use a while true loop, and while I came up with a simpler way to make this code work, I don't quite know where to start in making a while true loop for this problem.

The code I have so far is as follows:

    def check(word):
        pos = 0
        while True:
            if pos < len(word):
                if word[pos:pos+2] == "in":
                    pos += 2 
                    break
                if word[pos:pos+2] == "ex":
                    pos += 2
                    break
                if word[pos:pos+3] == "are":
                    pos += 3 
                    break
        return "YES"

It's only half of the code that I'm missing (so far it only works for words that have all the syllables).

Aucun commentaire:

Enregistrer un commentaire