在猪拉丁语翻译中修复While循环

时间:2015-10-02 00:56:04

标签: python python-3.x while-loop

我正在研究猪拉丁语翻译计划:

  1. 提示用户输入一个字词。
  2. 如果单词以元音开头,请将“way”添加到单词的末尾。
  3. 如果单词以辅音开头,请从单词的开头删除所有辅音,并将它们附加到单词的末尾。然后,将“ay”附加到单词的末尾。
  4. 用户输入“退出”时停止(大小写字母的任意组合,例如“退出”,“退出”或“qUIt”)
  5. 我的程序满足1-3就好了,但我似乎无法弄清楚如何不断提示用户输入一个单词,除非他们输入某种形式的"退出"。我知道我需要使用while语句,但这是我提出的最好的:

    while True:
    
    VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
    
    statement = input("Please enter a word: ")
    statementL = statement.lower()
    
    if statementL == "quit":
        print ("Exiting program")
        break
    
    
    def find_vowel(word):
       for i in range(len(word)):
         if word[i] in VOWELS:
            return i
       return -1
    
    words =  statement.split()
    count = 0
    
    for word in words:
       vowel = find_vowel(word)
    # No vowels found
       if(vowel == -1):
          print (word)
    # A vowel is the first letter
       elif(vowel == 0):
        print (word + "way")
       else:
    # A consonant is first
        print (word[vowel:] + word[:vowel] + "ay")
    

    我当前的while循环不断提示用户输入一个单词但不翻译它,当我尝试将程序块退出到底部时出现错误。有什么建议?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我不太清楚问题是什么。我为角色复制了你的代码字符并且它正确运行。可能存在的唯一问题是您认为在while循环中的代码可能不是。检查你的缩进。这是我运行的代码:

while True:

    VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")

    statement = input("Please enter a word: ")
    statementL = statement.lower()

    if statementL == "quit":
        print ("Exiting program")
        break


    def find_vowel(word):
       for i in range(len(word)):
         if word[i] in VOWELS:
            return i
       return -1

    words =  statement.split()
    count = 0

    for word in words:
       vowel = find_vowel(word)
       # No vowels found
       if(vowel == -1):
          print (word)
       # A vowel is the first letter
       elif(vowel == 0):
          print (word + "way")
       else:
          # A consonant is first
          print (word[vowel:] + word[:vowel] + "ay")