0

thanks for checking this out. So I am having a few problems.

  1. I can't seem to get the desired output into another file. I am trying to simulate a receipt in a sense to show the description of the item, quantity, item cost, etc.. It doesn't seem to work right and usually puts a negative in there. I know that it has to do with entering a negative number to stop but I am kind of confused on this.

  2. I can't seem to format correctly either. I want to round everything to two decimal places which I know is {:.2f} but, since I am trying to line these up in columns I can't seem to figure this out.

My Code:

def cashRegister():
    subTotal = 0
    total = 0
    salesTax = .085
    registerTableHeader = "{0:<15}{1:>35}{2:>10} {3:>10}{4:>10}\n"\
                           .format('Description', 'Qty', 'Unit Cost',\
                             'Item Cost', 'Subtotal')
    cashRegisterFile.write(str(registerTableHeader))


    quantityOfItem = int(input("Enter quantity of item: "))
    while(quantityOfItem > 0):
        itemDescription = str(input("Enter Item's description: ")[:30])
        itemCost = float(input("Enter item's cost: "))        
        subTotal = quantityOfItem * itemCost 
        total += subTotal
        quantityOfItem = int(input("Enter quantity of item: "))

    registerTable = "{0:<15}{1:>35}{2:>10} {3:>10} {4:>10}\n\n"\
            .format(itemDescription, quantityOfItem, itemCost, itemCost * \
                quantityOfItem, subTotal)      
    cashRegisterFile.write(str(registerTable))

Desired Output:

Description                     Qty Unit Cost Item Cost  Subtotal
First Item's Description          1     24.99     24.99     24.99
This is the second item's desc    5      2.00     10.00     34.99
This is the third item's descr   10     11.00    110.00    144.99

Sales Tax                                                   13.05
Total                                                      158.04

Thanks for looking at my post!

1 Answer 1

1

You need to indent your writing of your items so that it will be executed fo each item in the while loop. You wrote only the item after the loop. Hence your negative number from th condition while(quantityOfItem > 0): had ben written to file.

I change the top of the loop to:

    while True:
        quantityOfItem = int(input("Enter quantity of item: "))
        if quantityOfItem <= 0:
            break

Format your numbers like {4:>10.2f} for two decimals:

def cashRegister(cashRegisterFile):
    subTotal = 0
    total = 0
    salesTax = .085
    registerTableHeader = ("{0:<30} {1:>10}{2:>10} {3:>10} {4:>10}\n".format(
            'Description', 'Qty', 'Unit Cost','Item Cost', 'Subtotal'))
    cashRegisterFile.write(registerTableHeader)

    while True:
        quantityOfItem = int(input("Enter quantity of item: "))
        if quantityOfItem <= 0:
            break
        itemDescription = input("Enter Item's description: ")[:30]
        itemCost = float(input("Enter item's cost: "))        
        subTotal = quantityOfItem * itemCost 
        total += subTotal

        registerTable = ("{0:<30} {1:>10}{2:>10.2f} {3:>10.2f} {4:>10.2f}\n".format(
                itemDescription, quantityOfItem, itemCost, itemCost * quantityOfItem,
                subTotal))   
        cashRegisterFile.write(registerTable)

with open('cash.txt', 'w') as cashRegisterFile:
    cashRegister(cashRegisterFile)

Content of cash.txt:

Description                           Qty Unit Cost  Item Cost   Subtotal
A long description of my produ          3      4.00      12.00      12.00
a                                       2      4.00       8.00       8.00
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for helping! The only problem I have is that when I set the columns it won't stay. I have it set where it will only show the first 30 characters of the description but it will still move the rest of the string down the line. I can't quite figure that one out.
Changed the formatting strings. Should work now. See my updated answer.
Great that it helped. BTW, you can accept an answer if it solves your problem.
Sorry I didn't get back to you, I just did it! Thanks again for helping me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.