Joe han
Joe han

Reputation: 171

Writing text file in vertical order

I have this program calculate module to calculate the sub‐total for each item and total based on the price:

#calculate
def calculate1():
    a4canon = (int(input('A4 paper (canon):')))*8.9
    a4rainbow = (int(input('A4 paper (rainbow):')))*7.5
    lruler = (int(input('Long ruler:')))*0.85
    sruler = (int(input('Short ruler:')))*0.55
    blue = (int(input('Blue pen:')))*0.65
    red = (int(input('Red pen:')))*0.65
    black = (int(input('Black pen:')))*0.65
    pencil = (int(input('2B Pencil:')))*2.4

    total = a4canon + a4rainbow + lruler + sruler + blue + red + black + pencil

    a = str(a4canon)
    b = str(a4rainbow)
    c = str(lruler)
    d = str(sruler)
    e = str(blue)
    f = str(red)
    g = str(black)
    h = str(pencil)

    i = str(total)

    return [('A4 paper(canon):',a),('A4 paper(rainbow):',b),('Long Ruler:',c),
            ('Short Ruler:',d),('Blue Pen:',e),('Red Pen',f),('Black Pen:',g),
            ('2B Pencil:',h),('Total:',i)]

and display module: prompt out customer’s name and proceed to record the purchase into a textfile:

#display
import calculate
def display1(x):
    file = open('sample.txt','w')
    file.write(input('Customer name:'))
    lst = []
    lst = x
    for i in lst :
        file.write('\n'.join(list(i)))
    print('Your order is recorded. Thank you and please come again.')

and file module:

#file
import display
def file1(x):
    while True:
        user = input('Do you want to keep a record (y/n):')
        if (user == 'y') or (user == 'Y'):
            display.display1(x)
            break

        elif (user == 'n') or (user == 'N'):
            print('Thank You. Please come again')
            break

        else:
            print('Wrong input. Please try again.')

and Main module: to coordinate and manage all modules to perform the task:

import menu
import calculate
import file
import display

menu.menu1()
value = calculate.calculate1()
file.file1(value)

it seems that the data inside the file is not save in order:

bob8.9
A4 paper(canon):7.5
A4 paper(rainbow):Long Ruler:
0.85Short Ruler:
0.55Blue Pen:
0.65Red Pen
0.65Black Pen:
0.652B Pencil:
2.4Total:
22.149999999999995

What should I do to make the data it saves to be like this:

customer Name:bob
A4 paper(canon):1 ~ 8.9
A4 paper(rainbow):1 ~ 7.5
Long Ruler:1 ~ 0.85
Short Ruler:1 ~ 0.55
Blue Pen:1 ~ 0.65
Red Pen:1 ~ 0.65
Black Pen:1 ~ 0.65
2B Pencil:1 ~ 2.4
Total:22.149999999999995

Upvotes: 1

Views: 1663

Answers (2)

martineau
martineau

Reputation: 123531

The ordering issue is being caused by the file.write(input('Customer name:')) lacking a \n at the end. Needs to be file.write(input('Customer name:')+'\n').

That alone won't give you the output desired however, because the list returned from calculate1() doesn't contain the quantity and product prices. While this could be fixed by hardcoding a bunch of things into it, I think it would be better to everything more "data-driven" which will simplify the code and make it easier to modify.

It does this though a new (ordered) dictionary I added called PRICE_LIST which contains the product names and prices. The rest of the code uses this to to control the processing it does as much as it possible.

Here's what I mean (note: all the code had been put into single file to make presenting the changes simpler):

from collections import OrderedDict

PRICE_LIST = OrderedDict((
    ('A4 paper(canon)', 8.9),
    ('A4 paper(rainbow)', 7.5),
    ('Long Ruler', 0.85),
    ('Short Ruler', 0.55),
    ('Blue Pen', 0.65),
    ('Red Pen', 0.65),
    ('Black Pen', 0.65),
    ('2B Pencil', 2.4),
))

def calculate1():
    print('Enter the desired quanities of each item.')
    cart = {product: int(input(product+'?: ')) for product in PRICE_LIST}
    total = sum(cart[product] * PRICE_LIST[product] for product in cart)
    return [(product, cart[product], PRICE_LIST[product])
                for product in PRICE_LIST] + [('total', 1, total)]

def display1(lst):
    file = open('sample.txt', 'w')
    cust = input('Customer name: ')
    file.write(cust+'\n')
    for product, quanity, price in lst:
        file.write('{}: {} ~ {}\n'.format(product, quanity, price))
    print('Your order is recorded. Thank you and please come again.')

def file1(x):
    while True:
        user = input('Do you want to keep a record (y/n)?: ')
        if (user == 'y') or (user == 'Y'):
            display1(x)
            break
        elif (user == 'n') or (user == 'N'):
            print('Thank You. Please come again')
            break
        else:
            print('Wrong input. Please try again.')

value = calculate1()
file1(value)
print('done')

Sample run:

Enter the desired quanities of each item.
A4 paper(canon)?: 1
A4 paper(rainbow)?: 1
Long Ruler?: 1
Short Ruler?: 1
Blue Pen?: 1
Red Pen?: 1
Black Pen?: 1
2B Pencil?: 1
Do you want to keep a record (y/n)?: y
Customer name: Bob
Your order is recorded. Thank you and please come again.
done

Contents of sample.txt file afterwards:

Bob
A4 paper(canon): 1 ~ 8.9
A4 paper(rainbow): 1 ~ 7.5
Long Ruler: 1 ~ 0.85
Short Ruler: 1 ~ 0.55
Blue Pen: 1 ~ 0.65
Red Pen: 1 ~ 0.65
Black Pen: 1 ~ 0.65
2B Pencil: 1 ~ 2.4
total: 1 ~ 22.149999999999995

Upvotes: 0

Casey Knauss
Casey Knauss

Reputation: 131

1) I don't think there is any reason to import menu I don't see it doing anything.

2) I don't think you need to import display in the main.py

try changing

file.write('\n'.join(list(i)))

to

file.write('\n{} {}'.format(i[0], i[1]))

this is my output

Tom

A4 paper(canon): 8.9

A4 paper(rainbow): 7.5

Long Ruler: 0.85

Short Ruler: 0.55

Blue Pen: 0.65

Red Pen 0.65

Black Pen: 0.65

2B Pencil: 2.4

Total: 22.149999999999995

Upvotes: 3

Related Questions