#python pig latin while loop

27 messages · Page 1 of 1 (latest)

random tendon
#

I have this working locally as a stand alone Python program.

When I change it into a function in the online editor, it works until a word makes it to the while loop, then it seems to go into an endless loop on the while statement, and I cannot see what I've done wrong.

Any suggestions, please?

    words = text.lower()
    words = words.split()#words = list from string 'text'
    vowels = ('aeiou')
    consonants = ('bcdfghjklmnpqrstvwxyz')
    for x in words:
        word = str(x)
        if word[0] in vowels: #Rule 1
            word1 = (word+'ay')
        elif (word[:2] == 'xr') or (word[:2] =='yt'): #Rule 1
            word1 = (word+'ay') 
        elif word[0] == 'y':
            word1 = (word[1:]+word[0]+'ay')
        elif word == 'my':
            word1 = (word+'ay')
        elif 'qu' in word[:3]:
            i = word.rindex('qu')
            word1 = word[i+2:]+word[:i+2]+'ay'
        else:
            #word1 = word
            while word[0] in consonants: 
                word = word[1:]+word[0]
            word1 = (word+'ay')      
        text = text.replace(word, word1)
    return text```
scarlet tangle
#

Try to put in a print statement inside the while loop and debug from there
Also, word of precaution, testing locally should mean testing against the downloaded test cases via the cli, otherwise "running fine locally" won't mean much because we have no idea what kind of example or test did you use to run the function

fading edge
#

Sharing how it fails would be helpful. The test output/details makes it much easier for us to help you.

#

str.replace() doesn't respect word boundaries so that's a very iffy way to change words.

scarlet tangle
random tendon
#

Oh, and the while loop ran fine on stand-alone code.

scarlet tangle
#

Oh, and the while loop ran fine on stand-alone code.
refer back to my earlier comment

smoky jasper
#

if text is not lowercase to startwith, text.replace(word, word1) won't replace anything.

random tendon
smoky jasper
#

Did you do exercism download -t python -e pig-latin?

random tendon
smoky jasper
#

yes, but you don't write text = text.lowercase(). If text contains "Hello", then word will be "hello", and text.replace won't find it.

#

surely not the cause of the infinite loop tho

smoky jasper
#

I'd not bother modifying the input parameter. Create a new list to contain the pigified words, then return that list joined.

scarlet tangle
#

hah, i like pigified

random tendon
scarlet tangle
#

yeah it is the same thing, gleen's one is shorter and less explicit

#

afterward you should run pytest and see which one pass and which one didnt

random tendon
# scarlet tangle afterward you should run pytest and see which one pass and which one didnt

Ok, so I'm new to most of this, especially functions and args, etc. I'm going to attach my Python stand alone...```#def translate(text):
print("Text string, please: ")
text = input("")

words = text.lower()
words = words.split()#words = list from string 'text'

vowels = ('aeiou')
consonants = ('bcdfghjklmnpqrstvwxyz')
#specials = ('xray', 'yttria')
#print("Line 8: vowels = ", vowels, "type: ", type(vowels))
#print("Line 9: consonants = ", consonants, "type: ", type(consonants))

for x in words:
word = str(x)
if word[0] in vowels: #Rule 1
word1 = (word+'ay')
elif (word[:2] == 'xr') or (word[:2] =='yt'): #Rule 1
word1 = (word+'ay')
elif word[0] == 'y':
word1 = (word[1:]+word[0]+'ay')
elif word == 'my':
word1 = (word+'ay')
elif 'qu' in word[:3]:
i = word.rindex('qu')
word1 = word[i+2:]+word[:i+2]+'ay'
else:
word1 = word
while word1[0] in consonants:
word1 = word1[1:]+word1[0]
word1 = (word1+'ay')
text = text.replace(word, word1)
print(text)
#return text```

fading edge
#

What happens when you run pytest? Please share output using a codeblock and not images

random tendon
fading edge
#

I didn't see any test results in any codeblocks. Did I miss that somewhere?

scarlet tangle
fading edge
#

The online editor runs pytest and shows you formatted results. Any way of "testing" the code that doesn't use pytest is almost certainly not actually testing the right thing.

#

It's not helpful to check "does my code do X" when the exercise is asking you to write code that does Y.