+ 1
string filtering
Hi, The code supposes to filter vowels and consonants from a given word. filtered vowels and consonants suppose to be printed as: word_vowels and word_consonats code looks as follow: What is wrong? word = 'pasjonat' def alphabet_filter(word): vowels = ('a', 'e', 'i', 'o', 'u') word_vowels = '' for x in word: if x in vowels: word_consonants = word.replace(x, '') word = word_consonants for y in word: if y not in vowels: vowel = str(y) word_vowels += vowel print(word_consonants) print(word_vowels) alphabet_filter(word)
2 Réponses
+ 5
I would recommend you to rework the code, so that it gets more readable:
# version 1:
vow = 'aeiou'
word = 'pasjonat'
vowels = []
consonants = []
for char in word:
if char in vow:
vowels.append(char)
else:
consonants.append(char)
print(''.join(vowels))
print(''.join(consonants))
# or version 2 as a comprehension:
vowels = [x for x in word if x in vow]
consonants = [x for x in word if x not in vow]
print(''.join(vowels))
print(''.join(consonants))
+ 1
thank you Lothar! your code makes the case much easier to understand !