简体   繁体   English

在Python中查找单词的第一个元音

[英]Finding first vowel of a word in Python

so I am having trouble finding the index of the first vowel of a inputted string. 所以我找不到输入字符串的第一个元音的索引。 When putting in the string "elephant" , "hello" , "spa" they work properly, however when I input "spam" it does not work, it returned the number 3 instead of 2. I am having trouble finding why it satisfies the else statement but not the initial if condition. 当输入字符串“elephant”,“hello”,“spa”时它们正常工作,但是当我输入“垃圾邮件”它不起作用时,它返回数字3而不是2.我很难找到为什么它满足else语句但不是初始if条件。 I am also trying to put a condition that if there are no vowels in the string, then it should print out the index of the last character in the string. 我也试图设置一个条件,如果字符串中没有元音,那么它应该打印出字符串中最后一个字符的索引。 Below is my code: 以下是我的代码:

def find_first_vowel(word):
    i = 0   
    while i < len(word):
        i+= 1
        if word[i] in vowels:
            return i
        else:
            return len(word)-1
    return i

print(find_first_vowel("spam"))   

Your code always return len(word)-1 if the character in position 1 is not a vowel. 如果位置1的字符不是元音,则代码始终返回len(word)-1 Also elephant didn't work and spa works only because of the bug I mentioned, it returns 2 which is len(word)-1 and not the index of the vowel found. elephant也没有工作, spa工作只是因为我提到的错误,它返回2len(word)-1而不是找到的元音的索引。 Try debug your code line by line and you'll figure it out very quickly. 尝试逐行调试您的代码,您将很快找到它。

This could be a working code that return -1 if there's no vowel, else return the index of the first vowel found. 这可能是一个工作代码,如果没有元音则返回-1 ,否则返回找到的第一个元音的索引。

def find_first_vowel(word):
    i = 0   
    while i < len(word):
        if word[i] in vowels:
            return i
        i += 1
    return -1

EDIT 编辑

If you want to return the last character if there's no vowels just change return -1 with return len(word) - 1 . 如果你想在没有元音的情况下返回最后一个字符,只需改变return -1return len(word) - 1 Here: 这里:

def find_first_vowel(word):
    i = 0   
    while i < len(word):
        if word[i] in vowels:
            return i
        i += 1
    return len(word) - 1

You should use enumerate to handle tracking the index for you 您应该使用enumerate来处理跟踪索引

vowels = set('aeiou')

def find_first_vowel(word):
    for index, letter in enumerate(word):
        if letter in vowels:
            return index
    return index  # Returns last index if no vowels.  You could also return None, or raise an error

Either return statement jumps out of your while loop immediately, without looking at the rest of the word. return语句会立即跳出while循环,而不会查看其余部分。 Additional problem: since you start with i += 1 , the function never even looks at the first character, which would be word[0] . 附加问题:因为你从i += 1 ,该函数甚至不会查看第一个字符,即word[0]

I think your main issue is that you meant for your else statement to be executed if the while loop completed and exited without finding a vowel. 我认为你的主要问题是,如果while循环完成并退出而没有找到元音,那么你的意思就是执行你的else语句。 As indented, though, it's part of the if statement in the while loop, and executes whenever the second character isn't a vowel. 但是,缩进时,它是while循环中if语句的一部分,并且只要第二个字符不是元音就会执行。 You want something more like this: 你想要更像这样的东西:

i = 0
while i < len(word):
    if word[i] in vowels:
        return i
    i += 1

And then whatever you want the function to return if the word has no vowels. 如果单词没有元音,那么无论你想要什么功能都可以返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM