Practice Makes Perfect 8/15 What Im I doing wrong?


#1

My code outputs:

HHHHHHHHHHeeeeeeeeeyyyyyyyyyy YYYYYYYYYYooooooooouuuuuuuuu!!!

And I get the error:

Your function fails on anti_vowel(“Hey look Words!”). It returns “HHHHHHHHHHeeeeeeeeeyyyyyyyyyy llllllllllooooooooooooooooookkkkkkkkkk WWWWWWWWWWooooooooorrrrrrrrrrddddddddddssssssssss!!!” when it should return “Hy lk Wrds!”.

My code:

def anti_vowel(text):
vowels = “aeiouAEIOU”
newstr = “”
for letter in text:
for vowel in vowels:
if letter != vowel:
newstr += letter

return newstr
print anti_vowel(“Hey You!”)

What am I doing wrong?

My new code which manages to print HEy You!

def anti_vowel(text):
vowels = “aeiouAEIOU”
newstr = “”
for letter, vowel in zip(text, vowels):
if letter != vowel:
if letter.upper() != vowel.upper():
newstr += letter

return newstr
print anti_vowel(“HeEy You!”)

So Im almost there, but why does it remove the one vowel, but not the other three (E,o and u)?


#2

for vowel in vowels in vowel will loop over all the vowels, this loops determines if letter is a vowel. So after the for vowel in vowels you want to add letter to newstr, if letter has never equaled vowel


#3

you thinking about my first code?

Isn’t it what I’ve done?

for letter in text:
for vowel in vowels:
if letter != vowel:
newstr += letter

And Isn’t my second code a bit better considering Im using zip() that compares two things…?


#4

the problem with your first code is that the current letter will not equal most vowels, so you don’t want to add to newstr each time if letter != vowel evaluates to true

lets say letter is l

l will not equal: a, e, i, o, u, A, E, I, O, U

so for each of these vowels, l will appended to newstr

you only want to append the letter l once after determining it doesn’t equal any of the vowels


#5

Yes, so thats why Im trying to focus on my second code that actually manages to remove one of the vowels…how can I improve on the second code so that it manages to remove the rest of the vowels?


#6

using zip() will not work, look:

for letter, vowel in zip(text, vowels):
   print letter, vowel

so unless letter and vowel are exactly on the same index, which might be one in a million, its never good enough


#7

Yeah I just saw that hehe, but then Im back to square one :frowning:


#8

the in keyword can be very handy here, you can use it to determine if letter is in vowels string

of course you could use a loop like you attempt, but the loop is more difficult to get the logic right


#9

I did this and managed to remove everything except the vowels, so Im getting closer lol

def anti_vowel(text):
vowels = “aeiouAEIOU”
newstr = “”
for letter in text:
if letter in vowels:
newstr += letter
return newstr
print anti_vowel(“Hey You!”)


#10

And now I did this and it worked lol

def anti_vowel(text):
vowels = “aeiouAEIOU”
newstr = “”
for letter in text:
if letter not in vowels:
newstr += letter
return newstr
print anti_vowel(“Hey You!”)


#11

yep, you need to reverse the condition. Just like ! you used earlier. But this time you will need a keyword, to check that letter is not in vowels

yep, not allows us to reverse the condition. Nicely done


#12

Hahaha but i didn’t even know that we can put not inside IF like this…they never showed that to us during all the lessons so far…or maybe I have a really bad memory


#13

Negate would be a more applicable term, one thinks.


#14

Wow look at the Codecademy code:

def anti_vowel(text):
t=""
for c in text:
for i in “ieaouIEAOU”:
if c==i:
c=""
else:
c=c
t=t+c
return t
print anti_vowel(“Hey You!”)

So simple…in a good way…


#15

Which we can further reduce to,

def anti_vowel(text):
  t=""
  for c in text.lower():
    if c in "aeiou":
      c = ""
    t += c
  return t

print anti_vowel(“Hey You!”)

And we can see where NOT simplifies it even further.

if c not in 'aeiou': t += c

#16

But what about the uppercase letters, if we want to check both?

isn’t it better to drop the lower() and keep the for loop without it?

for c in text:
if c in “ieaouIEAOU”:

either that or apply both lower() and upper()?


#17

‘Better’ is a subjective term. At this stage whatever works is sufficient. Our review and research will point us to a variety of solutions. The main thing is to think through the problem and make sure we understand what is expected in the outcome. How we get there is not for us or anybody else to judge. If it works, it works.

We could use the whole vowel string, if we choose.


#18

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