字符串错误输入

时间:2014-02-22 21:23:02

标签: python string printing

我无法打印正确的输出。这是我的输出:

ere-Hay
are-ay
he-tay
ords-way

这是我期待得到的:

ere-Hay
are-ay
e-thay
ords-way

我的代码,某些东西不适合toPigLatin:

isVowels = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')   

def toPigLatin(word):
    letter1 = word[0]
    if letter1 in isVowels:
        return word + "-ay" 
    else:
        return (word[1:] + "-" + word[0] + "ay")   

def main():
    #filename = input("Enter filename: ")
    filename = "some.txt"

    for word in open(filename):
        print(toPigLatin (word.strip()))

main()

这是我的some.txt:

Here
are
the
words

任何帮助都会很棒。谢谢。

1 个答案:

答案 0 :(得分:0)

isVowels = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')___

def toPigLatin(word):
    letter1 = word[0]
    if letter1 in isVowels:
        return word + "-ay"_
    else:
        prefix = ''
        for letter in word:
            if letter in isVowels:
                break
            else:
                prefix += letter
        return (word[len(prefix):] + "-" + prefix + "ay")___

def main():
    #filename = input("Enter filename: ")
    filename = "some.txt"

    for word in open(filename):
        print(toPigLatin (word.strip()))

main()

这会产生输出:

ere-Hay
are-ay
e-thay
ords-way