我该如何匹配元音? [英] How do I match vowels?
本文介绍了我该如何匹配元音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理正在开发的较大程序的一小部分.基本上,我需要让用户输入一个单词,并且需要打印第一个元音的索引.
I am having trouble with a small component of a bigger program I am in the works on. Basically I need to have a user input a word and I need to print the index of the first vowel.
word= raw_input("Enter word: ")
vowel= "aeiouAEIOU"
for index in word:
if index == vowel:
print index
但是,这不起作用.怎么了?
However, this isn't working. What's wrong?
推荐答案
尝试:
word = raw_input("Enter word: ")
vowels = "aeiouAEIOU"
for index,c in enumerate(word):
if c in vowels:
print index
break
for .. in
将遍历字符串中的实际字符,而不是索引. enumerate
将返回索引以及字符,并使引用两者更加容易.
for .. in
will iterate over actual characters in a string, not indexes. enumerate
will return indexes as well as characters and make referring to both easier.
这篇关于我该如何匹配元音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文