辅音卡在Pig Latin中

时间:2018-05-21 16:53:50

标签: python-3.x

我的代码问题在这里。当我输入ONLY辅音时,程序会卡住。但是当我输入混音辅音和元音时,它按预期运行。

例如:

  1. apple - > appleway
  2. 是 - > isway
  3. a - >远
  4. scratch - > atchscray
  5. b - >卡住就像什么都没发生一样
  6. 感谢您是否可以帮助我。谢谢。

    Deque

1 个答案:

答案 0 :(得分:0)

最简单的解决方案(不过多修改代码)是检查一个单词没有元音时的特殊情况。

ignoreCase = "quit"
vowels = "aeiou"

while True:
    # Get word and convert to lower case
    word = input("Enter a word ('quit' to quit): ").lower()

    if word == ignoreCase: 
        break
    elif word == "":               
        print("Can't convert empty string.  Try again.")
    else:
        # Starts with a vowel
        if word[0] in vowels:
            print(word + "way") 

        # Starts with a consonant
        else:
            # Only run loop if word has vowels
            if any(char in vowels for char in word):
                while word[0] not in vowels:
                   word = word[1:] + word[0] 
            print(word + "ay")