Why doesn't the .pop function remove letters "u" and "U" from the list vowels when the input contains "Qu"?
Question:
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"))
Answers:
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.
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()
.
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.
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"))
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.
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()
.
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.