将收据打印到外部文本文件

时间:2013-12-07 02:49:15

标签: python

我已经制作了一个打印餐馆收据的程序,但是如何将整个收据打印到外部txt或dat文件?我尝试使用这种方法但它只能打印出print语句而不是if和for语句中的计算..

outfile = open("receipt.txt" , 'w')
outfile.write('Kenny\'s Deluxe Cafe Receipt\n')
outfile.write('Hello world')

我的收据

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 > 0:
    outfile.write("DRINK\n")
    if coffee > 0:
        d1['Coffee               '] = (coffee, countcoffee) # d[menu_item] = (cost, order_count)
    if juice > 0:
        d1['Juice                '] = (juice, countjuice) # d[menu_item] = (cost, order_count)
    if soda > 0:
        d1['Soda                 '] = (soda, countsoda) # d[menu_item] = (cost, order_count)
    if water > 0:
        d1['Water                '] = (water, countwater) # d[menu_item] = (cost, order_count)
    if lemonade > 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 > 0:
    outfile.write("Subtotal for drinks:         ${:.2f}\n".format(totaldrinks))
    outfile.write("\n")
if totalsalads > 0:
    outfile.write("SALADS\n")
    if veggiesalad > 0:
        d2['Vegetable Salad      '] = (veggiesalad, countvsalad) # d[menu_item] = (cost, order_count)
    if italiansalad > 0:
        d2['Italian Salad        '] = (italiansalad, countisalad) # d[menu_item] = (cost, order_count)
    if chickensalad > 0:
        d2['Chicken Salad        '] = (chickensalad, countcsalad) # d[menu_item] = (cost, order_count)
    if tunasalad > 0:
        d2['Tuna Salad           '] = (tunasalad, counttsalad) # d[menu_item] = (cost, order_count)
    if rainbowsalad > 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 > 0:
    outfile.write("Subtotal for salad:          ${:.2f}\n".format(totalsalads))
    outfile.write("\n")
if totalsandwiches > 0:
    outfile.write("SANDWICHES\n")
    if chickensandwich > 0:
        d3['Chicken Sandwich     '] = (chickensandwich, countcsandwich) # d[menu_item] = (cost, order_count)
    if turkeybacon > 0:
        d3['Turkey Bacon Sandwich'] = (turkeybacon, countttbsandwich) # d[menu_item] = (cost, order_count)
    if tunasandwich > 0:
        d3['Tuna Sandwich        '] = (tunasandwich, counttsandwich) # d[menu_item] = (cost, order_count)
    if veggiesandwich > 0:
        d3['Vegetable Sandwich   '] = (veggiesandwich, countvsandwich) # d[menu_item] = (cost, order_count)
    if meatballsub > 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 > 0:
    outfile.write("Subtotal for sandwiches:     ${:.2f}\n".format(totalsandwiches))
    outfile.write("\n")
if totaldesserts > 0:
    outfile.write("DESSERTS\n")
    if cccookie > 0:
        d4['5x Chocolate Chip Cookie'] = (cccookie, countcccookie) # d[menu_item] = (cost, order_count)
    if brownie > 0:
        d4['5x Brownie              '] = (juice, countjuice) # d[menu_item] = (cost, order_count)
    if cheesecake > 0:
        d4['Cheesecake              '] = (cheesecake, countcheesecake) # d[menu_item] = (cost, order_count)
    if tiramisu > 0:
        d4['Tiramisu                '] = (tiramisu, counttiramisu) # d[menu_item] = (cost, order_count)
    if lemonpie > 0:
        d4['Lemonpie                '] = (lemonpie, countlemonpie) # d[menu_item] = (cost, order_count)
    if dccake > 0:
        d4['Dark Chocolate Cake     '] = (dccake, countdccake) # d[menu_item] = (cost, order_count)
    if applepie > 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 > 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()

这就是我所做的,它起作用了。即时通讯。 >。<

1 个答案:

答案 0 :(得分:0)

将所有print命令更改为outfile.write命令。并在底部添加outfile.close()