Home > Enterprise >  Why doesn't the .pop function remove letters "u" and "U" from the list vowe
Why doesn't the .pop function remove letters "u" and "U" from the list vowe

Time:10-16

If the word begins with the letters "qu", the letter "u"/"U" should be removed from the list vowels, however, my code doesn't work. How can I fix this?

For context, this code is for converting English to Pig Latin.

vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
qu = ["qu", "Qu"]
def firstVowelIndex(w):
    for index, char in enumerate(w):
        if char in vowels:
            return index


def encryptVow(w):
    w = w   "-way"
    return w

def encryptCon(w):
    wordToList = list(w)
    if wordToList[0:2] in qu:
        vowels.pop(4)
        vowels.pop(9)
        index = firstVowelIndex(w)
        return w[index:]   "-"   w[:index]   'ay'

    vowels.append("y")
    vowels.append("Y")
    index = firstVowelIndex(w)
    return w[index:]   "-"   w[:index]   'ay'

def encrypt(w):
    wordToList = list(w)
    if wordToList[0] in vowels:
        return encryptVow(w)

    elif wordToList[0] not in vowels:
            return encryptCon(w)



if __name__ == '__main__':

    print(encrypt("quiz"))

CodePudding user response:

I'd say this should do what you asked in your question.

if word.lower().startswith("qu"):
    vowels.remove("U")
    vowels.remove("u")

P.S: You do not have to turn a string into a list because a string is an iterable of characters. As such you can subindex it without needing to change datatype. As such you could have also used something like:

if word[0:2].lower() == "qu":
    vowels.remove("U")
    vowels.remove("u")

But I'd say using the string method startswith() is more explicit.

Also consider that if you use the same vowels list for multiple words. The code above will raise a ValueError cause effectively you already removed those vowels before.

CodePudding user response:

First, you are using vowels as a global variable. It is OK since you call encrypt() only once. But if you use it multiple times, once the letters 'u' and 'U' are removed, they will not be present after a word starting with 'q' has been encountered.

Each time the function encrypt() is called, it appends 'y' and 'Y' multiple times.

Method pop() cannot be called with a string, it must be called with an integer. Use search().

Here is a working version of your program (I added a few calls to encrypt()).

vowels =  ['a','e','i','o','u','A','E','I','O','U']

def firstVowelIndex(w):
  for index, char in enumerate(w):
    if char in vowels:
      return index

def encryptVow(w):
  return w   '-way'

def encryptCon(w):
  if w.lower().startswith('q'):
    vowels.pop(vowels.index("u"))
    vowels.pop(vowels.index("U"))
    index = firstVowelIndex(w)
  else:
    vowels.append("y")
    vowels.append("Y")
    index = firstVowelIndex(w)
  return w[index:]   '-'   w[:index]   'ay'  
   
def encrypt(w):
  if w[0] in vowels:
    return encryptVow(w)
  else:
    return encryptCon(w)


if __name__ == '__main__':
    print(encrypt("quiz"))
    print(encrypt("art"))
    print(encrypt("boat"))
    print(encrypt("part"))

It prints the following results

iz-quay
art-way
oat-bay
art-pay

However, add print(vowels) at the start of the firstVowelIndex() function. You will see how the vowels list changes.

Consult python documentation about global variables.

  • Related