?Funny? error with anti vowel


#1

I'm not really sure why its doing this, and i reversed my if condition so it only keeps the vowels that worked....

  def anti_vowel(text):
      keep = ""
      bad = ["a","A","e","E","i","I","o","O","u","U"]
      for t in text:
          for b in bad:
              if t != b:
                  keep += t
      return keep

I get a crazy return like: "....returns "HHHHHHHHHHeeeeeeeeeyyyyyyyyyy llllllllllooooooooooooooooookkkkkkkkkk WWWWWWWWWWooooooooorrrrrrrrrrddddddddddssssssssss!!!!!!!!!!" when it should return "Hy lk Wrds!"."
I now know alternative methods to solve this, but I'm curious as to why this is happening.


#3

I wasn't looking for a solution to the lesson. I would like to better understand what is actually happening within the code I provided causing it behave as such, if anyone is able to explain.


#4

@caboose922,

  def anti_vowel(text):
      keep = ""
      bad = ["a","A","e","E","i","I","o","O","u","U"]
      # you created a list =bad= with 10 Elements
      for t in text:
          # you have taken 1 Character from the =text=
          for b in bad:
              #you are picking 1 Element from the =bad= list
              #you will be doing this picking 10 Times for every t-Character  
              if t != b:
                  keep += t
                  #if you have the character 'W' you would have concatenated 10 times
                  #if you have the character 'o' you would have concatenated 9 times !!!
      return keep

print anti_vowel('Hey look words!')

I get a crazy return like: "....returns
"HHHHHHHHHH(10)eeeeeeeee(9)yyyyyyyyyy(10) llllllllll(10)oooooooooooooooooo(2*9)kkkkkkkkkk(10) WWWWWWWWWWooooooooorrrrrrrrrrddddddddddssssssssss!!!!!!!!!!"

=======================================

def anti_vowel(text):
    keep = ""
    bad = ["a","A","e","E","i","I","o","O","u","U"]
    for t in text:
        is_bad = False
        for b in bad:
            if t == b:
                is_bad = True
                break
        if not is_bad:
            keep += t
    return keep

print anti_vowel("Hey look words!")

Doing a google search
== discussions / opinions ==
python check contain in list site:stackoverflow.com

found
# http://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exist-in-a-list

def anti_vowel(text):
    keep = ""
    bad = ["a","A","e","E","i","I","o","O","u","U"]
    for t in text:
        if not (t in bad): keep += t
#        is_bad = False
#        for b in bad:
#            if t == b:
#                is_bad = True
#                break
#        if not is_bad:
#            keep += t
    return keep

print anti_vowel("Hey look words!")

#5

Ah, I see it now. thank you for the detailed response and site link