8/15 with a while statement


#1

Hey guys, I would really appreciate any feedback on the code below - it returns the error -
Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "H" when it should return "Hy lk Wrds!".

Thanks a lot!

def anti_vowel(text):
    text_str = str(text)
    check = "aeiouAEIOU"
    n = len(text_str)
    count = 0 
    final = ""
    while count < n:
        for i in check:
            if i != text_str[count]:
                final = final + text_str[count]
            return final
        count = count + 1

#2

Hi donurjack!

Plug in your code with an example (like anti_vowel("Hey look Words!")) in this website:
Python Tutor
When you hit 'Visualize Execution" you can watch step-by-step how the program is processing your code.

I don't want to give the answer away, but what's happening is your code is comparing 'a' (the first letter in check) with 'H" in "Hey look Words!" Next it adds 'H" to final, but then it hits return final and jumps out of the loop.

Actually a simpler way might be using an expression to see whether each element in the passed phrase (i.e. each letter in "Hey look Words!") is in check. You could even eliminate the while loop and the counter this way (because the looping only lasts as long as there is an element in text_str). Maybe something like:

 for i in text_str:
     if i not in check:
         final = final + i

Just make sure return final is unindented . Give it a try, and good luck!


#3

Joeb,

Thanks a lot for your help! The visualiser is very good tool!

I realised that there was a simpler way but since it's an exercise I decided to try something more complicated just to see if I will be able to do it.

Thanks again for your time.