title: Print receipt to an external txt file
body: <p>I've made a program that will print a receipt of a restaurant but how do I go about printing the entire receipt to an external txt or dat file? I tried using this approach but it can only print out print statements and not the calculations in if and for statements.. </p>
  
  <pre><code>outfile = open("receipt.txt" , 'w')
  outfile.write('Kenny\'s Deluxe Cafe Receipt\n')
  outfile.write('Hello world')
  </code></pre>
  
  <p>My receipt</p>
  
  <pre><code>outfile = open("receipt.txt" , 'w')
  #sorting
  d1 = {}
  d2 = {}
  d3 = {}
  d4 = {}
  #receipt
  outfile.write("\n")
  outfile.write("Kenny's Deluxe Cafe Receipt\n")
  outfile.write("\n")
  outfile.write("   ***********************\n")
  outfile.write("   *    Regular Items    *\n")
  outfile.write("   ***********************\n")
  outfile.write("\n")
  if totaldrinks &gt; 0:
      outfile.write("DRINK\n")
      if coffee &gt; 0:
          d1['Coffee               '] = (coffee, countcoffee) # d[menu_item] = (cost, order_count)
      if juice &gt; 0:
          d1['Juice                '] = (juice, countjuice) # d[menu_item] = (cost, order_count)
      if soda &gt; 0:
          d1['Soda                 '] = (soda, countsoda) # d[menu_item] = (cost, order_count)
      if water &gt; 0:
          d1['Water                '] = (water, countwater) # d[menu_item] = (cost, order_count)
      if lemonade &gt; 0:
          d1['Lemonade             '] = (lemonade, countlemonade) # d[menu_item] = (cost, order_count)
  d1_lst = sorted(d1.items(), key=lambda x:x[1], reverse = True)
  for item in d1_lst:
      outfile.write("%ix %s     $%.2f\n"%(item[1][1], item[0], item[1][0]))
  if totaldrinks &gt; 0:
      outfile.write("Subtotal for drinks:         ${:.2f}\n".format(totaldrinks))
      outfile.write("\n")
  if totalsalads &gt; 0:
      outfile.write("SALADS\n")
      if veggiesalad &gt; 0:
          d2['Vegetable Salad      '] = (veggiesalad, countvsalad) # d[menu_item] = (cost, order_count)
      if italiansalad &gt; 0:
          d2['Italian Salad        '] = (italiansalad, countisalad) # d[menu_item] = (cost, order_count)
      if chickensalad &gt; 0:
          d2['Chicken Salad        '] = (chickensalad, countcsalad) # d[menu_item] = (cost, order_count)
      if tunasalad &gt; 0:
          d2['Tuna Salad           '] = (tunasalad, counttsalad) # d[menu_item] = (cost, order_count)
      if rainbowsalad &gt; 0:
          d2['Rainbow Salad        '] = (rainbowsalad, countrsalad) # d[menu_item] = (cost, order_count)
  d2_lst = sorted(d2.items(), key=lambda x:x[1], reverse = True)
  for item in d2_lst:
      outfile.write("%ix %s     $%.2f\n"%(item[1][1], item[0], item[1][0]))
  if totalsalads &gt; 0:
      outfile.write("Subtotal for salad:          ${:.2f}\n".format(totalsalads))
      outfile.write("\n")
  if totalsandwiches &gt; 0:
      outfile.write("SANDWICHES\n")
      if chickensandwich &gt; 0:
          d3['Chicken Sandwich     '] = (chickensandwich, countcsandwich) # d[menu_item] = (cost, order_count)
      if turkeybacon &gt; 0:
          d3['Turkey Bacon Sandwich'] = (turkeybacon, countttbsandwich) # d[menu_item] = (cost, order_count)
      if tunasandwich &gt; 0:
          d3['Tuna Sandwich        '] = (tunasandwich, counttsandwich) # d[menu_item] = (cost, order_count)
      if veggiesandwich &gt; 0:
          d3['Vegetable Sandwich   '] = (veggiesandwich, countvsandwich) # d[menu_item] = (cost, order_count)
      if meatballsub &gt; 0:
          d3['Meatball Sub         '] = (meatballsub, countmsandwich) # d[menu_item] = (cost, order_count)
  d3_lst = sorted(d3.items(), key=lambda x:x[1], reverse = True)
  for item in d3_lst:
      outfile.write("%ix %s     $%.2f\n"%(item[1][1], item[0], item[1][0]))
  if totalsandwiches &gt; 0:
      outfile.write("Subtotal for sandwiches:     ${:.2f}\n".format(totalsandwiches))
      outfile.write("\n")
  if totaldesserts &gt; 0:
      outfile.write("DESSERTS\n")
      if cccookie &gt; 0:
          d4['5x Chocolate Chip Cookie'] = (cccookie, countcccookie) # d[menu_item] = (cost, order_count)
      if brownie &gt; 0:
          d4['5x Brownie              '] = (juice, countjuice) # d[menu_item] = (cost, order_count)
      if cheesecake &gt; 0:
          d4['Cheesecake              '] = (cheesecake, countcheesecake) # d[menu_item] = (cost, order_count)
      if tiramisu &gt; 0:
          d4['Tiramisu                '] = (tiramisu, counttiramisu) # d[menu_item] = (cost, order_count)
      if lemonpie &gt; 0:
          d4['Lemonpie                '] = (lemonpie, countlemonpie) # d[menu_item] = (cost, order_count)
      if dccake &gt; 0:
          d4['Dark Chocolate Cake     '] = (dccake, countdccake) # d[menu_item] = (cost, order_count)
      if applepie &gt; 0:
          d4['Apple Pie               '] = (applepie, countapplepie) # d[menu_item] = (cost, order_count)
  d4_lst = sorted(d4.items(), key=lambda x:x[1], reverse = True)
  for item in d4_lst:
      outfile.write("%ix %s  $%.2f\n"%(item[1][1], item[0], item[1][0]))
  if totaldesserts &gt; 0:
      outfile.write("Subtotal for desserts:       ${:.2f}\n".format(totaldesserts))
      outfile.write("\n")
  outfile.write("7% TAX:                      ${:.2f}\n".format(tax))
  outfile.write("                             -------\n")
  outfile.write("TOTAL BALANCE:               ${:.2f}\n".format(finaltotal))
  outfile.write("------------------------------------\n")
  outfile.write("\n")
  outfile.write("Have a great day!")
  
  outfile.close()
  </code></pre>
  
  <p>this is what I did and it worked lol. im so noob. >.&lt;</p>