Python循环程序

Python循环程序,python,python-2.7,for-loop,Python,Python 2.7,For Loop,到目前为止,以下程序的要点是从字母表列表中辅音的位置迭代到字母表列表的末尾,以获得最接近的1个元音。如果循环找到最接近(右侧)的元音,则循环应停止迭代,并将该元音的索引和字母分配给aftervouel和aftervouelindex变量。对每个辅音也应该这样做(也就是说,如果当前输入(单词)的迭代是辅音,我们将其放在字母表列表中,然后在字母表列表中迭代,找到最靠近右元音的字母,然后停止) 问题是,它在字母表列表中迭代,并将所有元音输出到字母表上辅音的右侧,而不是仅输出一个元音 不幸的是,我尝试了

到目前为止,以下程序的要点是从
字母表
列表中辅音的位置迭代到
字母表
列表的末尾,以获得最接近的1个元音。如果循环找到最接近(右侧)的元音,则循环应停止迭代,并将该元音的索引和字母分配给
aftervouel
aftervouelindex
变量。对每个辅音也应该这样做(也就是说,如果当前输入(
单词
)的迭代是辅音,我们将其放在
字母表
列表中,然后在
字母表
列表中迭代,找到最靠近右元音的字母,然后停止)

问题是,它在
字母表
列表中迭代,并将所有元音输出到字母表上辅音的右侧,而不是仅输出一个元音

不幸的是,我尝试了使用break,而使用条件循环和其他技术,但没有任何效果

如何解决这个问题

PS.下面的
print
语句用于检查是否输出了一个或多个元音;这不是该计划的实际部分

def rovarspraket(word = raw_input("Please enter a word: ")):

    consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
    vowels = ['a','e','i','o','u']
    alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

    #1. Identify if the current element is a consonant, the consonant, and the consonant's index on alphabet list
    for character in range(len(word.lower())):
        for c in range(len(consonants)):
            if word[character] == consonants[c]:
                currentConsonant = word[character]

            #2. Determine After and Before vowels

                #After vowel 
                for a in range(alphabet.index(word[character]), len(alphabet)): 
                    for aV in range(len(vowels)):
                        if alphabet[a] == vowels[aV]:
                            afterVowel = alphabet[a]
                            afterVowelIndex = a
                            print afterVowel, afterVowelIndex 

我希望我能正确理解你的问题。如果我是,你就不能用一个布尔标志来解决这个问题吗

def rovarspraket(word = raw_input("Please enter a word: ")):

consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
vowels = ['a','e','i','o','u']
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

#1. Identify if the current element is a consonant, the consonant, and the consonant's index on alphabet list
for character in range(len(word.lower())):
    for c in range(len(consonants)):
        if word[character] == consonants[c]:
            currentConsonant = word[character]

        #2. Determine After and Before vowels

            #After vowel
            flag = False
            for a in range(alphabet.index(word[character]), len(alphabet)): 
                for aV in range(len(vowels)):
                    if alphabet[a] == vowels[aV] and not flag:
                        afterVowel = alphabet[a]
                        afterVowelIndex = a
                        print afterVowel, afterVowelIndex
                        flag = True

break
将退出一个循环。你需要一个旗子来突破第二个障碍:

done = False

for a in range(alphabet.index(word[character]), len(alphabet)): 
    for aV in range(len(vowels)):
        if alphabet[a] == vowels[aV]:
            afterVowel = alphabet[a]
            afterVowelIndex = a
            print afterVowel, afterVowelIndex
            done = True
            break

    if done:
        break

对“u”之后的字符做了什么?我们认为“A”最右边吗?另外,如果单词中的一个字符是元音,我们是输出该值还是输出该值之后的下一个元音?如果用户输入一个数字或符号怎么办?我们相信用户不会这样做吗?这和问题完全一样吗?@ljetibo看起来像是一个完全重复的问题。真是巧合…@Vincenzzochi把大部分程序都抛在了一边,正如你和其他人正确指出的那样,我和我的搭档在两个不同的账户上问了两次同样的问题,但没有得到回应,因为我们被告知要缩小问题的范围。所以不用担心“u”,它已经处理好了。如果单词中的字符是元音,则会跳过它,我们相信用户只会输入小写字母。这不是最佳解决方案,因为它在找到元音后会不断迭代。虽然这是真的,
break
是更好的解决方案,但他说他尝试了,但没有成功(不管是真是假),所以我想换一种方式。但是是的,OP,试着让
中断
工作。你能解释一下你用“标记”做了什么吗?我是编程新手,以前从未处理过打破嵌套循环的问题。您是否使用break中断了第二个循环,然后使用if done:break中断了第一个循环?