Need help with anti_vowel


#1



Practice Makes Perfect: anti_vowel


The error that was displayed said, "Your code looks a bit off--it threw a "'str' object has no attribute 'remove'" error. Check the Hint if you need help!"


I am seriously lost as to what I am suppose to do for this lesson. Am I on the right track or am I not close? Why does the .remove command not work? Is it because I need to change text into a different form, such as a list? Thanks in advance!


def anti_vowel(text):
    for i in text:
        if i == 'a'or'e'or'i'or'o'or'u'or'A'or'E'or'I'or'O'or'U':
            text.remove(i)


#2

You could probably get the remove statement to work (not sure how, I'm new too)... BUT...

We're often encouraged not to modify the object we are iterating through while iterating through it - i.e, while you're "for" looping "text", don't change "text". Maybe build a new list instead.


#3

I thought about doing that, but I arrived on the conclusion that I was not sure how to convert the components of text into a list form.


#4

You could copy it to a new list. From reading other posts on here, I know that y = x does not make a second copy of y called x, in Python, but simply gives you two different names to access the same object, and changing x will also change y. However, you can apparently do something like x = list(y) to copy it. My tactic for this exercise was to create an empty string and then populate it with the desired results.


#5

So, I tried this and it gave me this error, "Your function fails on anti_vowel("Hey look Words!"). It returns "['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']" when it should return "Hy lk Wrds!"."


#6

In my experience, "if x conditional y or z or q or w" doesn't work for z or q or w, but instead needs to be phrased as "if x conditional y or x conditional z or x conditional..." which is a lot of typing. If you read the instructions/hint, I'm pretty sure they clue you in on an easier way ("is in"...)

Also, note: you are defining x as a copy of text, and then immediately thereafter redefining it as an empty list. You only need to do one or the other.


#7

def anti_vowel(text):
x = []
for i in text:
if i in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
x.append(i)
return x

With this code it says,"Your function fails on anti_vowel("Hey look Words!"). It returns "['H', 'y', 'l', 'k', 'W', 'r', 'd', 's']" when it should return "Hy lk Wrds!"." Why does it look like this?


#8

Well, you returned a list (which explains the brackets and commas.) If you return a string, it won't look like that.

Additionally, a bit more efficiently than listing every consonant letter, you could use "not" and list vowels.


#9

Perhaps the method that I am trying to use simply does not work for this problem. It now generated the response,"Your function fails on anti_vowel("Hey look Words!"). It returns "HylkWrds" when it should return "Hy lk Wrds!"." I'm not sure how I am suppose to keep the original spacing without going about this problem a completely different way.

def anti_vowel(text):
x = []
for i in text:
if i in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
x.append(i)
return ''.join(x)


#10

You're getting super close.

In my tactic, I didn't even go into the land of lists, and kept everything in strings. It was a bit simpler, but your way is not impossible. The issue is with what separator you're using - the join function is probably ignoring the spaces between words. You may want to debug by printing your new list before you join it into a string, to see that the spaces are indeed present before 'join', and then look up 'join' to see if there are different arguments you can feed it to preserve it.

Otherwise, you can make your new list an empty string instead.


#11

Upon trying some things, I found that if I add str to my x. (Making the ending part str(x) instead of ''.join(x).) It had the same result as not adding str had. Why is this so?


#12

Join automatically produces a string, so turning it into a string first is redundant.


#13

I removed the .join function and replaced it with str.


#14

Also, how did you only keep your code in strings when you can't use .append on strings?


#15

Oh, I see. Well, similarly, ''.join() and str() are different ways of doing the same thing; join gives you more options, from what I understand, than str(). Str() may be simpler to type and work most of the time, and then there may be circumstances where you require join in order to control the separators etc.


#16

So, I tried a different method where I didn't use lists and it still game me the same error, the one that said I didn't have any spaces.

def anti_vowel(text):
x = ''
for i in text:
if i in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
x+=str(i)
return x


#17

I am using "del" instead of remove.Hope this helps.

def anti_vowel(text):
t = "" # defining a empty string
for i in text: # iterating through each letter in the text,
if i in "aAeEiIoOuU": # comparing each letter with vowels
del i # if matches remove letter
else:
t += i # else add each letter to empty string
return t # Returning empty string

print anti_vowel("Hey look Words!")


#18

Thanks for the help! I now got it. :smiley:


#19

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