如果没有单词或替代文本,如何显示元音

时间:2018-06-08 15:05:17

标签: python

list = ["amita","aman","ishita","rythm"]
k = 0
for str in list:
    print (str)
    for ch in str:
        if(ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u'):
            print(ch)
        else:
            print("there is not any vowel in the word")

我想显示每个单词的元音。如果单词中没有任何元音,则应该只显示"there is not any vowel into the word"一次。但上面的代码会多次显示"there is not any vowel into the word"列表"rhythm"中的姓氏{{1}}。

3 个答案:

答案 0 :(得分:4)

这种使用集合交叉的方法将在每个单词中打印每种类型的元音,忽略重复的元音(如果这是可接受的输出):

names = ["amita","aman","ishita","rythm"]
vowels = set("aeiouy")

for name in names:
    print(name)
    intersect = set.intersection(set(name), vowels)
    if intersect:
        print("\n".join(intersect))
    else:
        print("there is not any vowel in the word")

答案 1 :(得分:1)

试试这个

vowels = ["a", "e", "i", "o", "u"]
list = ["amita","aman","ishita","rythm"]
for str in list:
    print (str)
    vowel_count = 0
    for ch in str:
        if ch in vowels:
            print(ch)
            vowel_count += 1
    if vowel_count == 0:
        print("there is not any vowel in the word")

答案 2 :(得分:1)

您可以使用旗帜,也可以试试

list = ["amita","aman","ishita","rythm"]
vowels = ['a', 'e', 'i', 'o', 'u']# you can add y

for str in list:
    print(str)
    contains_vowel = False
    for ch in str:
        if ch in vowels:
            print(ch)
            contains_vowel = True
    if not contains_vowel:
        print("there is not any vowel in the word")