首页 > 解决方案 > 以列表为参数的迭代不考虑每个字符

问题描述

我正在尝试创建一个程序,您可以输入一个单词并告诉您上面的元音和辅音,我尝试使用元音列表但是当我运行程序时它只打印 else 或辅音参数。我'我是 python-3.x 和一般编程的新手。我究竟做错了什么?。

def run(word,list_vocal):
    for letter in word:
        if letter == list_vocal:
           print(letter + 'is a vowel')
        else:
           print(letter + 'is a consonant')
if __name__ == '__main__':
   word = str(input('Type your word ')
   list_vocal = ['a', 'e' , 'i', 'o'. 'u']
   run(word,list_vocal)

我只考虑西班牙元音。

这是输出.

标签: pythonpython-3.x

解决方案


您正在检查 astring是否等于 a listwhich will never be true

而是检查它是否是in列表:

def run(word,list_vocal):
    for letter in word:
        if letter in list_vocal:
           print(letter + 'is a vowel')
        else:
           print(letter + 'is a consonant')
if __name__ == '__main__':
   word = str(input('Type your word ')
   list_vocal = ['a', 'e' , 'i', 'o', 'u']
   run(word,list_vocal)

推荐阅读