import string
def main():
  filename = input("Enter the name of a file to translate into Pig Latin: ")
  vowels = ['a', 'e', 'i', 'o', 'u','A','E','I','O','U']
  regFile = open((filename), 'r')
  Output = open('Output.txt', 'w')
  for line in  regFile.readlines():
    pigList = line.split()
    t = translate(pigList, vowels)
    w = write(t, Output)
  regFile.close()
  input ("Press ENTER to continue: ")


def translate(pigList, vowels):
  PigList2 = []

  for word in pigList:
    if word[-1] in string.punctuation:
      actual_word = word[:-1]
      ending = word[-1]
    else:
      actual_word = word
      ending = ""

    if word[0] in vowels:
      PigList2.append(actual_word + "-yay" + ending)
    else:
      PigList2.append(actual_word[1:] + "-" + actual_word[0] + "ay" + ending)
  return PigList2

def write(pigList, Output):
  print(" ".join(pigList))

main()


我相信这已解决了我的错误。感谢您的帮助。我知道翻译器工作正常并且一次翻译所有行,而不是一次翻译一行。

最佳答案

你快到了。我只是使用标点符号检查将您的单词分为实际单词和标点符号,然后在第一个字母之前加上“-”,而不是“ ay”。

def translate(pigList, vowels):
  PigList2 = []

  for word in pigList:
    if word[-1] in string.punctuation:
      actual_word = word[:-1]
      ending = word[-1]
    else:
      actual_word = word
      ending = ""

    if word[0] in vowels:
      PigList2.append(actual_word + "-yay" + ending)
    else:
      PigList2.append(actual_word[1:] + "-" + actual_word[0] + "ay" + ending)

  return PigList2

关于python - Python Pig-Latin转换器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55144979/

10-11 04:10