Я создал программу, которая распечатает квитанцию о ресторане, но как я могу распечатать всю квитанцию во внешний файл txt или dat? Я попытался с помощью этого подхода, но он может печатать только операторы печати, а не расчеты, если и для операторов ..Распечатайте квитанцию во внешний txt-файл
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()
это то, что я сделал, и она работала лол. im so noob. >. <
Вы должны действительно отделить свою схему построения квитанций от логики написания файлов. Тогда это однострочная операция, а не многострочная. – roippi
Вы можете перенести это на codereview – samrap