python中的循环和字符串转换

python中的循环和字符串转换,python,Python,我是编程新手,我很难理解如何询问用户是否希望再次运行它 def pigLatin(word): """ Function that converts word into Pig Latin form """ # Checking for starting letter as vowel if word[0] in ['a','e','i','o','u']: return word + "way" # Checking for vowel

我是编程新手,我很难理解如何询问用户是否希望再次运行它

    def pigLatin(word):
     """
   Function that converts word into Pig Latin form
     """

  # Checking for starting letter as vowel
  if word[0] in ['a','e','i','o','u']:
   return word + "way"

  # Checking for vowel index
  for i in range(1, len(word)):
      # Checking for vowels(including y)
       if word[i] in ['a','e','i','o','u','y']:
       # Updating vowel index
       vowelIndex = i;
       break;

  # Forming pig lating word      
   return    word[vowelIndex:] + word[0:vowelIndex] + "ay";


 def removePunctuation(text):
  """
   Removes punctuation from text
  """

  # List of punctuation characters
  punctuations = ['(', ')', '?', ':', ';', ',', '.', '!', '/', '"', "'"]

   # Iterating over punctuations
   for ch in punctuations:
   # Replacing punctuations
   text = text.replace(ch, "")

   # Returning text
   return text


  def main():
  """
   Main function
   """
   # Reading line of text
   text = input("\n Enter a line of text: ")

   # Removing punctuation
   text = removePunctuation(text)

   # Converting to lower case
   text = text.lower()

 print("\n\n Pig Latin form: \n");

  # Iterating over words
  for word in text.split(" "):

   # Converting word to Pig Latin form
   word = pigLatin(word)

   # Printing word
   print(word, end = " ");

   print("\n");


  # Calling main function
  main()
我应该如何添加一个循环来请求用户继续或不继续?我是编程新手,我很困惑。我也不明白如何在pig拉丁形式之前添加英语形式的句子??
有人能帮忙吗

一个简单的解决方案可能如下所示:

while True:
    main()

    response = input("Continue?")
    if response != 'y':
        break
您可能希望对接受的输入不那么严格


我认为最好将这个循环放在主函数中(并在调用它之前检查一下
\uu name\uu
),但这至少演示了循环的外观。

您可以在
主函数中使用
while
循环和-1这样的字符串退出

def main():
   while True: 
   """
   Main function
   """
   # Reading line of text
   text = input("\n Enter a line of text or -1 to exit: ")
   if text == '-1':
      exit(0)
   # Removing punctuation
   text = removePunctuation(text)

   # Converting to lower case
   text = text.lower()

 print("\n\n Pig Latin form: \n");

  # Iterating over words
  for word in text.split(" "):

   # Converting word to Pig Latin form
   word = pigLatin(word)

   # Printing word
   print(word, end = " ");

   print("\n");

在我们查看您的代码之前,请修复您的缩进!使用一段时间,简单!欢迎来到SO。不幸的是,这不是一个讨论论坛或教程。请花点时间阅读它所包含的链接。花些时间练习这些例子。它将让您了解Python提供的帮助您解决问题的工具?你能给我看看@AakashVerma吗。这通常意味着你需要的是半个小时的时间和当地的导师在一起,或是走一走,而不是堆积如山。