Python社区  »  Python

python:附加记录不会附加到文件

WhiteGlint • 1 周前 • 7 次点击  

显示所有发票订单时,将显示的唯一发票记录是用于文件写入的选项1,而使用附加选项2的所有其他发票记录都不存在。选择选项3时,如何确保每个附加发票记录也将显示。(我知道我的编码很平庸)

run_again = 'y'

while run_again == 'y':
    widget = 10.55
    gidget = 7.30
    doodad = 5.25
    print('1 - Create a new Invoice Order')
    print('2 - Add an additional Invoice Order')
    print('3 - Display all Invoice Orders')
    print('4 - QUIT')
    choice = int(input('Enter 1,2,3, or 4: '))
    if (choice == 1):
                 invoice_file = open('invoices.txt', 'w')
                 ID = input('1 of 4 - Enter customer ID: ')
                 numwidgets = int(input('2 of 4 - Enter number of Widgets'
                                        ' Ordered @ $10.55 each: '))
                 numgidgets = int(input('3 of 4 - Enter number of Gidgets'
                                        '(Ordered @ $7.50 each): '))
                 numdoodads = int(input('4 of 4 - Enter number of Doodads'
                                        '(Ordered @ $5.25 each: '))
                 invoice_file.write(ID + '\n')
                 invoice_file.write(str(numwidgets) + '\n')
                 invoice_file.write(str(numgidgets) + '\n')
                 invoice_file.write(str(numdoodads) + '\n')

                 invoice_file.close()
                 print('Data has been added to file invoices.txt')

    if (choice == 2):
                 invoice_file = open('invoices.txt', 'a')
                 ID = input('1 of 4 - Enter customer ID: ')
                 numwidgets = int(input('2 of 4 - Enter number of Widgets'
                                        ' Ordered @ $10.55 each: '))
                 numgidgets = int(input('3 of 4 - Enter number of Gidgets'
                                        '(Ordered @ $7.50 each): '))
                 numdoodads = int(input('4 of 4 - Enter number of Doodads'
                                        '(Ordered @ $5.25 each: '))
                 invoice_file.write(ID + '\n')
                 invoice_file.write(str(numwidgets) + '\n')
                 invoice_file.write(str(numgidgets) + '\n')
                 invoice_file.write(str(numdoodads) + '\n')

                 invoice_file.close()
                 print('Data has been added to file invoices.txt')     
    if (choice == 3):
                 invoice_file = open('invoices.txt', 'r')
                 ID = invoice_file.readline()
                 numwidgets = float(invoice_file.readline())
                 numgidgets = float(invoice_file.readline())
                 numdoodads = float(invoice_file.readline())
                 records = 0
                 records += 1
                 widgettotal = widget * numwidgets
                 gidgettotal = gidget * numgidgets
                 doodadtotal = doodad * numdoodads
                 invoicetotal = widgettotal + gidgettotal + doodadtotal

                 print('Record #',records, 'Customer #',ID)
                 print(numwidgets, 'Widgets ordered @ $10.55---- $',widgettotal)
                 print(numgidgets, 'Gidgets ordered @ $7.30----$',gidgettotal)
                 print(numdoodads, 'Doodads orderd @ $5.25----$',doodadtotal)
                 print('Invoice total------------$',invoicetotal)


                 invoice_file.close()



    if (choice == 4):
                 break

    run_again = input('Would you like to run this program again?'
                      '(Enter y for yes or n for no): ')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40231
 
7 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 1 周前
Quinn Mortimer
Reply   •   1 楼
Quinn Mortimer    9 月前

我认为问题不一定是选项2中的行没有写入文件,而是它们没有作为选项3的一部分被读取。选项3中的代码将尝试读取文件的前四行,而不读取其他任何内容。相反,你可能想要这样的东西:

if choice == "3":
    invoice_file = open('invoices.txt', 'r')
    lines = invoice_file.readlines(4)
    records, num_widgets, num_gadgets, num_doodads = 0, 0, 0, 0
    while len(lines) == 4:
        records += 1
        num_widgets += 1
        # ...
        lines = invoice_file.readlines(4)

    # Do calculations, output, and close file