+ 1

Python code coach: Remove the vowels

Currently stuck on the 7.2 intermediate python lesson to remove vowels and have them print in brackets. Code: word = input() vowels = ('a', 'e', 'i', 'o', 'u') result = [] for letter in word: if letter not in vowels: result.append(letter) print(result) Input: Hello Expected result: ['h','l','l'] Actually result: ['h'] ['h'] ['h','l'] ['h','l','l'] ['h','l','l'] So while I do ultimately get the correct answer by the fourth iteration it runs a check on each line one letter at a time. I'm lost as to how to get it to print only the final line. TIA

26th Oct 2021, 7:37 PM
Mike Duris
Mike Duris - avatar
3 ответов
+ 5
Move the print statement from inside the loop to after the loop.
26th Oct 2021, 7:39 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Thank you, I didn't think about the nesting.
26th Oct 2021, 7:45 PM
Mike Duris
Mike Duris - avatar
0
vow = ['a', 'e', 'i', 'o', 'u'] word = input() list = [i for i in word if i not in vow] print(list)
21st May 2022, 7:12 AM
Junior Jackson
Junior Jackson - avatar