Anti_vowel Help please!


#1



This is my code, it doesn't work somehow and I have been stuck for a month.

def anti_vowel(text):
    refined = ""
    some_number = 0
    count = 0
    for i in range(len(text)):
        for text[count] in "aeiouAEIOU":
            if c.lower() != "a" or "e" or "i" or "o" or "u":
                refined += c
        count += 1
    "".join(refined)
    return refined
        
print anti_vowel("word")
            
      I was working and turned my code into:
(it didn't work)

def anti_vowel(text):
    refined = ""
    vowels = ""
    some_number = 0
    count = 0
    while count < len(text):
        text[count] = str(text[count])
        if text[count].lower() == "a"or"e"or"i"or"o"or"u":
            vowels += text[count]
        elif text[count].lower() != "a"or"e"or"i"or"o"or"u":
            refined += text[count]
        count += 1
    "".join(refined)
    return refined
    
print anti_vowel("word")


#2

Comments on your code:

def anti_vowel(text):
    refined = ""
    some_number = 0 # Never used this anywhere else
    count = 0 #could be extra
    for i in range(len(text)):
        for text[count] in "aeiouAEIOU": #extra loop may not be needed. You're trying to see if letter is a vowel? Your first loop already loops through text. Why not just turn this into an if statement. This line may not be needed.
            if c.lower() != "a" or "e" or "i" or "o" or "u": #So now you're saying if letter isn't vowel? Can use "not in" eg. if text[x] not in "abcdefABCDEF" (Similar to the line above).
                refined += c
        count += 1 #probably extra
    "".join(refined) #this is to turn a LIST into STRING. But you're always dealing with STRING so this is extra
    return refined
        
print anti_vowel("word")

Your variable c in c.lower() was never defined. Do you mean to use text[i]?
Use the knowledge of loops and indexes. for item in list means for every item in list. item becomes an "index" so you can do list[item] to bring out EACH item of the list per iteration. Same with letters.
for letter in range(len(text)): means every letter in text. text[letter] brings out EACH letter in text per iteration.

If you plan to not use not in for

if c.lower() != "a" or "e" or "i" or "o" or "u":

You'll need to do

if c.lower() != "a" or c.lower() !="e" or c.lower() !="i" or c.lower() !="o" or c.lower() !="u":

#4

Thanks!!! I appreciate it!!!


#5

Hello there, I tried to write a code but it seems it doesn't work and I don't understand why.
Could you help me please ? I've tried two ways:

def anti_vowel(text):
    refined = ""
    for letter in range(len(text)):
            if letter.lower() != "a" or letter.lower() !="e" or letter.lower() !="i" or letter.lower() !="o" or letter.lower() !="u": 
                refined += letter
    return refined
        
print anti_vowel("word")
``` 

and 


```
def anti_vowel(text):
    refined = ""
    for letter in range(len(text)):
            if letter != "abcdefABCDEF" 
                refined += letter
    return refined

#6

First of all your for loop.

This produces out integer indexes. If you want each letter, you will need to do text[letter].
There is a way to loop through to get EACH letter in text and that is to simply to

for letter in text:

This way, you don't need to do text[letter] in your if statement below but you can leave it as letter.

Another thing is the if statement of the first one is too long. Best to go with second code. In your second code, you should use not in instead of !=. As != will say if letter is not abcdefABCDEF instead of saying whether letter is not one of the letters in abcdefABCDEF.

Also change that to aeiouiAEIOU. Don't forget the colon : at the end of your if statement.

IF you want to use the first one, in the if statement, change all or to and.

or means that no matter how may Falses you get, if one of them is True, the letter will be added. Such as say you have the letter "o", yes letter.lower != "o" will be False but letter.lower() != "a" will be True because "o" isn't "a". Therefore, the whole statement is True and o gets added in.


#7

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