8. anti_vowel


#1



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


This is my code:

def anti_vowel(text):
	for i in text:
		if i != "a" and  i != "e" and i != "i" and i != "o" and i != "u" and i != "A" and i != "E" and i != "I" and i != "O" and i != "U":
			return i
print anti_vowel("Hey You")


The output turns out to be "H" which is expected to be "Hy Y".
Can anyone tell me what has gone wrong with my code?
Thank you very much.


#2

This code here is very monotonous and it is not DRY

To avoid writing all that you can shorten it by just writing,

if i not in "aeiouAEIOU":

But by doing that you would need an empty string variable before your loop like this,

def anti_vowel(text):
    empty_string = " "
    for i in text:
        if i not in "aeiouAEIOU":

So now all that's left is to add your filtered contents to your empty string, and then add a return statement that's on the same indent level as your for loop. :grin:


#3

Thank you for solving my problem::blush:


#4

@erminefossil That's quite tedious to write out all the time. Also:

if i != "a" and  i != "e" and i != "i" and i != "o" and i != "u" and i != "A" and i != "E" and i != "I" and i != "O" and i != "U":
			return i

You only return things for functions, not if statements. Instead that should be replaced with printing i. Not to mention, your code doesn't follow the instructions, the instructions said to print the text given with all vowels removed. Your function only prints if the text given doesn't have vowels at all. Also, your code will look much cleaner if you do this:

def anti_vowel(text):
    word = '' #single quotes
    for letter in text:
        if not (letter in 'aeiouAEIOU'):
            word += letter
    return word

-- @chipjumper36731


#5

Here is my code, using replace() function and it is not DRY.


#6

did your code with replace() work?


#7

Yes.. it works.. cool


#8

You're welcome :grin:


#9

My code looks like this:

def anti_vowel(text):
    empty_string = " "
    for i in text:
        if i not in "aeiouAEIOU":
            return i
print anti_vowel("Hey You!")

but it doesn't seem to work


#10

type or paste code here
```I got my code sorted so that it sort of works. My code looks like this:
```
def anti_vowel(text):
    empty_string = " "
    for i in text:
        if i not in "aeiouAEIOU":
            print i
anti_vowel("Hey look Words!")

but it continuously prints:
H
y

l
k

W
r
d
s
!
None

what should I do about this, because I don't want to completely copy someone else's code.


#11

add comma(,) after print i which will print the letters horizontally with a space.


#12

For me, I'd like to write in this way:

def anti_vowel(text):
	empty_string = ""
	for i in text:
		if i not in "aeiousAEIOU":
			empty_string += i
	return empty_string
print anti_vowel("Hello World")

The output turns out to be: "Hll Wrld". There's no space between the characters:)


#13

Don't use return,coz return is a signal that ends your function,you can create a new empty strings,like this "",here is my code :blush:i do hope this work for you
def anti_vowel(text):
res = ""
for c in text:
if c not in "aeiouAEIOU":
res += c
return res


#14

instead of the print i function, you want to add your results to your empty_string = " "
empty_string += i will do this. Then you can just return empty_string for the results. You also want to remove the space in between " " or it will print the space when you return your results. This will cause your code to error.


#15

Here is mine. It eliminates the need for 'if' by using replace to search and replace. Very similar to boardrock, just shorter. My vowel definition can be shorter, but you know win some, lose some...

def anti_vowel(text):

vowels = ['a','e','i','o','u', 'A','E','I','O','U']

for i in vowels:
        text = text.replace(i,'')

return text

#16

Heres my code, a little longer but easy to follow:

def anti_vowel(text):
vowel = "aeiouAEIOU"
text_list = ""
n = len(text)-1
m = n - n
for char in text:
if char not in vowel:
text_list += text[m]
m += 1
else:
m += 1
return text_list


#17

After seeing other codes, I came up with this exact code structure, then saw yours.

Great minds!

def anti_vowel(text):
    empty_string = ""
    for i in text:
        if i not in "aeiouAEIOU":
            empty_string += i
    return empty_string
anti_vowel("Hey look Words!")

#18

Thank you !! :):slight_smile:


#19

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