-1

I'm doing GrokLearning NCSS Challenge Intermediate level and I'm wondering how to find out if the first character of each word in a list is a vowel, if it is then print it?

I've done this so far...

printing_words = []
word = input("Words: ")
b = word.split()
for w in b:
  if w[0] == "a" or "e" or "i" or "o" or "u":
    printing_words.append(w)
print(printing_words)

I expect an output of:

Words: ham ant egg apple banana
ant apple egg

In alphabetical order and all that

Help please?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
DasGuyDatSucks
  • 103
  • 1
  • 11
  • 2
    `if w[0] == "a" or "e" or "i" or "o" or "u"` is a common gotcha (see https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) and will always return `True`.... you'll be closer to what you want if you do `if w[0] in 'aeiou'` instead... – Jon Clements Aug 25 '19 at 06:42
  • You teeter on the edge of contravening the 'homework help' rules on SO (see https://stackoverflow.com/help/on-topic). Grok has specialist tutors that can help you, available directly on their site. They'll give you better quality help, and are almost certainly speedier with their responses. – GoatsWearHats Aug 27 '19 at 12:22

5 Answers5

1

You can also use in instead of or.

words = input("Words: ")

printing_words = list()
for word in words.split():
    if word[0] in 'aeiou':
        printing_words.append(word)

# sorting words in alphabetical order while removing brackets as it prints
print(*sorted(printing_words))
0

strs have method called startswith which accept tuple of allowed starts (prefixes), so you might use it following way:

if w.startswith(("a","e","i","o","u")):

Note that you might feed startswith with tuple of different-length strs.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

you can filter the words from your words list by checking if each lowered word starts with a vowel than you can sort them alphabetically:

sentence = input("Words: ")
words = sentence.split()

vowel = ("a","e","i","o","u")
print_words = sorted(filter(lambda w: w.lower().startswith(vowel), words))
print(*print_words)

output:

Words: ham ant egg apple banana
ant apple egg
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • `print(*print_words)` adding asterisk before list inside `print()` is more convenient than using `join()`. –  Aug 25 '19 at 07:35
-1

Try this:

printing_words = []
theWord = input("Words: ")
b = theWord.split()
for w in b:
   printing_words.append(w)
words_starting_with_vowel = [word for word in printing_words if word[0] in 'aeiou']
words_starting_with_vowel.sort();
print(words_starting_with_vowel);
Max Voisard
  • 1,685
  • 1
  • 8
  • 18
  • Code-only answers are not helpful. How does this code help solve the question asked? What is it doing? – GoatsWearHats Aug 27 '19 at 12:25
  • @GoatsWearHats What this code is doing is exactly what the question asked. It finds any words the user inputs that start with a vowel and then outputs a list of the words in alphabetical order. Why are you troubled with this code? Code-only answers are just as fine as regular answers. – Max Voisard Aug 27 '19 at 17:31
-1
word = input("Words: ")
printing_words = sorted([w for w in word.split() if w[0] in "aeiou"])
print(printing_words)
Words: ham ant egg apple banana
['ant', 'apple', 'egg']
taseikyo
  • 126
  • 7