Reputation: 1
So I defined a function that checks for vowels from a input and prints them in a list, it works but it prints multiple lists when I only want it to print one like (['i', 'e', 'e', 'a', 'e'])
Here is my code
vowels = 'aeiou'
sentence = input()
def VowelChecker(sentece):
for letter in sentence:
if letter in vowels:
print([letter])
Any ideas?
Upvotes: 0
Views: 141
Reputation: 26
Maybe you can create a new list and each time you find a letter, add it to that list. At the end, return that list, so that you can print it.
vowels = 'aeiou'
sentence = input()
def VowelChecker(sentece):
res = []
for letter in sentence:
if letter in vowels:
res.append(letter)
return res
print(VowelChecker(sentence))
Upvotes: 0
Reputation: 11602
You can also use a list comprehension:
vowels = 'aeiou'
sentence = "icebreaker"
def VowelChecker(sentece):
print([c for c in sentence if c in vowels])
VowelChecker(sentence)
# ['i', 'e', 'e', 'a', 'e']
Upvotes: 1
Reputation: 504
You need to create a list and then append to it, eventually printing it once after the loop:
def print_vowels(sentence):
vowels = 'aeiou'
vowels_in_sentence = []
for letter in sentence:
if letter in vowels:
vowels_in_sentence.append(letter)
print(vowels_in_sentence)
Another way to achieve this would be to use filter
builtin:
def print_vowels(sentence):
vowels = 'aeuio'
vowels_in_sentence = filter(lambda letter: letter in vowels, sentence)
print(vowels_in_sentence)
Upvotes: 1