我的代码到目前为止,但由于我太迷路了,它没有做任何接近我想要它做的事情:

vowels = 'a','e','i','o','u','y'
#Consider 'y' as a vowel

input = input("Enter a sentence: ")

words = input.split()
if vowels == words[0]:
    print(words)

对于这样的输入:
"this is a really weird test"

我只想打印:
this, is, a, test

因为它们只包含一个元音。

最佳答案

试试这个:

vowels = set(('a','e','i','o','u','y'))

def count_vowels(word):
    return sum(letter in vowels for letter in word)

my_string = "this is a really weird test"

def get_words(my_string):
    for word in my_string.split():
        if count_vowels(word) == 1:
            print word

结果:
>>> get_words(my_string)
this
is
a
test

关于python - 如何仅用1个元音打印单词?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15352119/

10-11 19:36