python - 以列表为参数的迭代不考虑每个字符
问题描述
我正在尝试创建一个程序,您可以输入一个单词并告诉您上面的元音和辅音,我尝试使用元音列表但是当我运行程序时它只打印 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)
我只考虑西班牙元音。
.
解决方案
您正在检查 astring
是否等于 a list
which 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)
推荐阅读
- javascript - 如何在悬停时将传单标记放在工具提示上?
- mongoose - 如何获取大型数据集上的查询结果总数?
- python - Choropleth 地图未显示但白页
- javascript - 使用 Axios 以 Formik 的多步形式上传图像时出错
- c++ - 我可以在“循环”有向图上使用 BGL 中的 dijkstra_shortest_paths
- reactjs - 如何在 React 中创建查找字段
- firebase - Flutter:如何在 [Firebase] 中使用 OR 条件获取流查询
- postgresql - Postgis + Qgis - 过滤行访问 - 插入和更新新点的问题
- flutter - 在 Flutter 中循环闪烁
- java - ORA-12516 Intellij 中的 ojdbc 问题