anti_vowel


#1

Code is below I keep getting error that it returns "Hy lok Wrds!" instead of "Hy lk Wrds!" why is it leaving that other "o" in look.

def anti_vowel(text):
    unvoweled = []
    for letter in text:
        unvoweled.append(letter)    
    
    ind = 0
    voweled = unvoweled
    
    for charac in voweled:
        if charac in "aeiouAEIOU":
            unvoweled.pop(ind)
            ind = ind + 1
        else:
            ind = ind + 1
    return "".join(unvoweled)

#3

It is simpler to loop over your string with:

for letter in text:

and append any letter that isn't a vowel, to an array.

There is a difficulty with your solution. lets say i have a really simple string:

print anti_vowel("book")

b is at index 0, your first o at index 1, second o at index 2 and the k on index 3. The moment the moment the first o is encountered index is 1, it pops the o. then the index increases (now your index is 2), but because of the pop, the second o of book has shift to index 1, and won't be removed. Removing like you do here, is an really difficult thing to do, because of what i just explained


#4

@stetim94, @datarockstar20306,
So would it be sufficient
to leave out
ind = ind + 1
after the pop ??


#5

no, it is not. ind doesn't control the index, the for loop is:

for charac in voweled:

ind is only the same as the index of the for loop because it increased by one each time the for loop runs (same as the loop)

found on stackoverflow:
for <var> in <iterable> merely runs the loop body with the <var> taking each value available from the iterable. Changing the <var> will have no effect on the ensuing iterations, so you can't, you would have to use a while loop of sort, give me a second to look at it


#6

@datarockstar20306 and @leonhard.wettengmx.n it is possible to make a solution with a pop, but it involve you use a range, rather then for charac in voweled:, plus, you would need a while loop (to remove one or more vowels if there are two or more vowels in a row), which will cause a index error if you have two vowels in a row, so you would need a try except to catch the index error. So yes, i can post the solution, but datarockstar is still working on the problem, so, no fun just getting the program. Datarockstar, i don't recommend the pop method, it is quit complicated


#7

I have a different code but similar issue.

vowel = 'aeiouAEIOU'
x = list(vowel)

def anti_vowel(text):
    y = list(text)
    for i in y:
        if i in x:
            y.remove(i)
    return ''.join(y)

print anti_vowel("Hey look Words!")

It keeps returning "Hy lk Words!". What should I fix in my code? I can't seem to fix it...


#8

Did you read my answer? remove and pop are very similar, it is really difficult to pull this off. you would first off need a try except to catch your indexerror you are going to get:

def anti_vowel(text):
    y = list(text)
    try:
        for i in y:
            if i in x:
                y.remove(i)
        return ''.join(y)
    except IndexError:
        return ''.join(y)

then, you would really need to use numbers rather then letters, otherwise you are never going to get there:

def anti_vowel(text):
    y = list(text)
    try:
        for i in range(0,len(y)):
            if y[i] in x:
                y.remove(y[i])
        return ''.join(y)
    except IndexError:
        return ''.join(y)

then you would need to construct a while loop rather then a if statement to catch any two consecutive (or more) vowels. Which is really tricky, it is much easier to just append all consonants to a list


#9

@janey_lee,
You are making the same mistake
as mentioned in the prior Posts.
As you use the remove() Method
on the underlying-list of your FOR-IN loop.

You are disrupting the =internal index-count= of the FOR-IN loop.

Have a look at the length of y ( 12 )
and the count of the iteration ( 7 )
and the count of the found ( 5 )

def anti_vowel(text):
    vowel = 'aeiouAEIOU'
    x = list(vowel)
    y = list(text)
    y_count = 0
    f_count = 0
    txt_len = str(len(y))
    for i in y:
        y_count += 1
        print "======="+txt_len
        print i + " y-count-" + str(y_count)
        if i in x:
            f_count += 1
            print str(f_count) +"-Found " + i
            print y
            y.remove(i)
            print y
    return ''.join(y)

#print anti_vowel("Hey look Words!")
print anti_vowel("!aeiouAEIOU!")

====================================


#10

Thank you, I don't completely understand the issue other than it's really hard to do this with remove or pop...but I tried with a different code and it seems to work fine in codecademy lab but not in the actual anti_vowel lesson window.


#11

Oops, hit reply too soon, here's the new code:

vowel = 'aeiouAEIOU'
p = []

def anti_vowel(text):
    for character in text:
        if character not in vowel:
            p.append(character)
    return ''.join(p)
print anti_vowel ('Hey look Words!')

Error Msg in the lesson window: Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Wrds!bcdHy lk Wrds!" when it should return "Hy lk Wrds!".


#12

@janey_lee,

As Python is strongly dependend on the proper indentation
you will have to re-edit your Post.....

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697/2
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

=========================

It could also be....
++++ reset of session *****************
under certain circumstances you can shoot your Browser in an
inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code
Then use the Reset Code button of the course-window,
then paste your code back in.

Addendum
General Notes:
Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).
CTRL 0 to reset browser zoom


#13

@leonhard.wettengmx.n, i formatted the code, i also get an error message, but the output looks good, any idea?