title: How to print output to text file from multiple functions (python)
body: <p>I want to save the below code output to <code>file.txt</code>. The problem is that my code includes many functions.</p>
  
  <p>In other words, I want my output in a text file, I'm using python but I posted as js, #python</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>import random
  
  
  Generate_Number_CHOICE = 1
  Average_Calculate_CHOICE = 2
  Display_Grades_CHOICE = 3
  Display_Failing_Marks_CHOICE = 4
  QUIT_CHOICE = 5
  
  def display_menu():
      print('        MENU')
      print('1) Enter Your Marks')
      print('2) Get Your Average')
      print('3) Display Your Grades')
      print('4) Display Failin Marks')
      print('5) Quit')
  
  
  def generate_number():
  
      numbers = random.sample(range(1 ,100),30)
      print(numbers)
      return numbers
  
  
  
  
  
  def average_calculate(numbers):
      total = sum(numbers)
      average = total // 30
      print('------------------------------------')
      print('Your Average is:', average)
      print(" ")
  
  
  
  
  def display_grades(numbers):
  
      for mark in(numbers):
          if mark  &gt;= 90:
           print('------------------------------------')
           print('A+',mark)
          elif mark  &gt;= 80:
           print('------------------------------------')
           print('B+',mark )
          elif mark  &gt;= 70:
           print('------------------------------------')
           print('C+',mark)
          elif mark  &gt;= 60:
           print('------------------------------------')
           print('D+',mark)
          else:
           print('------------------------------------')
           print('F',mark)
  
      return mark
      return courses_Name
  
  
  def display_failing(numbers):
      for number in (numbers):
              if number &lt;= 59:
               print('------------------------------------')
               print("you Fail in",number)
               print(" ")
  
  
  def main():
          infile = open('philosophers.txt', 'w')
  
          choice = 0
          while choice != QUIT_CHOICE:
  
              display_menu()
  
              choice = int(input('Enter your choice: '))
  
              if choice == Generate_Number_CHOICE:
                  numbers = generate_number()
  
  
  
              elif choice == Average_Calculate_CHOICE:
  
                  average_calculate(numbers)
  
              elif choice == Display_Grades_CHOICE:
  
                  mark = display_grades(numbers)
  
  
              elif choice == Display_Failing_Marks_CHOICE:
  
                  display_failing(numbers)
  
              elif choice == QUIT_CHOICE:
  
                  print('Exiting the program...')
  
              else:
  
                  print('Error: invalid selection.')
  
  main()</code></pre>
  </div>
  </div>
  </p>