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