Python Pig-拉丁语翻译器

标签 python

    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 Pig-拉丁语翻译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55144979/

相关文章:

python - file.write() 跳转到随机位置 python 2.7

python - 优化变更解决方案

python - 偏移数据框引用的最Pythonic方法?

python - 带洞的最长等差数列

python - 连接两个变量名称以自动在列表 python 中查找内容

python - 如何将一个函数的输出用作另一个函数的参数

python - plt.eventplot 拒绝线偏移

python - 如何验证 Google AppEngine 上的 Python 脚本以使用 Google Firebase?

python - 模糊匹配排名

python 异步特殊类方法 __delete__