Practice makes perfect anti_vowel


#1

Hello team,

can you tell me why my code doesn’t work and how to make it work?

def anti_vowel(text):
  vowel = ["A","E","I","O","U","a","e","i","o","u"]
  new_text = ""
  for i in text:
    if i in vowel:
      i = ""
      new_text = i
    else:
      new_text = text
  return new_text

Thank you!


#2

if i in vowel, you don’t want to do anything, else you want to append i (contain consonant) to new_text. Currently you just assign i to new_text, which will overwrite any values stored in new_text


#3

Now i tried the following but codeacademy give me an error:

AttributeError: ‘str’ object has no attribute ‘append’

def anti_vowel(text):
  vowel = ["A","E","I","O","U","a","e","i","o","u"]
  new_text = ""
  for i in text:
    if i in vowel:
      i = ""
      new_text = text.append(i)
    else:
      new_text = text
  return new_text

#4

if i is in vowel list, you don’t want to do anything. Given you don’t want to append a vowel.

i used the word append on purpose, to leave space for you to figure out how to concat strings, or use the appropriate data type to use the .append() method which belongs to list data type


#5

Different data types have different behaviour

Taking any data type and using its append method isn’t a sensible thing to do. It probably doesn’t have one at all.

When you do use some behaviour of a data type, you need to know what that behaviour is. Let’s say that the value you have is a list, that does indeed have an append method - but that method doesn’t have a return value, so assigning the result to a variable wouldn’t be useful because that variable now refers to nothing. (It’ll refer to the value None which represents the lack of value)
Instead, one would consider how list’s append method behaves, which is to modify the existing list, adding a value at the end. The result isn’t found in a new location, the original value changed.

By learning what the data types are you can make pretty good guesses about what behaviour they offer, but you’d still have to look up exactly how they’re used.

A list, for example, has a start location, and all the values are placed after that with equal size. This means that to find the 10th value you take the start location and add nine units of space, and that’s the location of your tenth value. (The first one is 0 units of space from the start)
This tells you things like that replacing or reading at a position is fast, just compute the location, read there, or overwrite there. But inserting/removing at the front is slow, because everything has to be moved. Inserting/removing at the back on the other hand is fast, because nothing has to move.

A string has some similar behaviour. But a big difference is that it can’t ever be changed. This makes it possible to use strings as keys in dictionaries. If one needs list-like behaviour, one would use a list. Since it can’t be changed, it cannot be appended to. New strings can be created by placing one after another, but this can be a whole lot of copying.


Another thing is that when you’re building a string, assigning the “new string” variable to each letter wouldn’t work. You’d be discarding the previous work. Some form of adding has to happen, to keep both everything from before, and the new letter.