8. anti_vowel


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096


I expect the x in text only be appended into the list if x!=y, however thats not the case. Is there any thing wrong with the double for loop? now every x in text goes into the list, even with the if x!=y statement


def anti_vowel2(text):
	list = []
	vowel = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
	for x in text:
            for y in vowel:
                if x != y:
		    list.append(x)
			
			
	return ''.join(list)

print anti_vowel2("abcdefg")


#2

Its syntax invalid, or is that just the formatting? If what you posted is different from what you used, then that's a pretty big problem in discussing the code.

Can you identify what it is doing differently from what it is supposed to? If so, what is that difference, don't make us find what you already know! Or if that's where you're stuck, explain that you need help identifying what is wrong about its behaviour.


#3

Sorry for the confusion, it's my first time making a post. I have edited the question. Many thanks in advance.


#4

You're comparing x to lots of y's, unless all y are the same and equal to x, then some of them will differ and cause you to append x

Watch out so you don't mix tabs and spaces by the way, that can be confusing. 4 spaces is usually used for indenting python code


#5

Thanks. I have amended the indent.

Can you explain more on for loop? How should I change my code to make it work? I know there's some other method for this. But I want to know if this double for loop gona work.

Below is another solution that I found for this exercise. How does it works but mine doesn't?

def anti_vowel(text):
    removed = text
    vowel = ["a","e","i","o","u","A","E","I","O","U"]
    for x in text:
        for y in vowel:
            if x == y:
                removed = removed.replace(x,"")
    return removed

#6

You can do whatever you can describe.
You're just not describing well enough what you imagine.

You need to decide exactly what events need to take place, and then write the code to match.

x is 'a':
is a != a? no
is a != e? yes, append
is a != i? yes, append
is a != o? yes, append
is a != u yes, append
is a != A? yes, append
is a != E? yes, append
is a != I? yes, append
is a != O? yes, append
is a != U? yes, append


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.