我有这个程序计算模块,根据价格计算每个项目的总分和总数:
#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)]
和显示模块:提示输出客户名称并继续将购买记录到文本文件中:
#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.')
和文件模块:
#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.')
和主模块:协调和管理所有模块以执行任务:
import menu
import calculate
import file
import display
menu.menu1()
value = calculate.calculate1()
file.file1(value)
似乎文件中的数据没有按顺序保存:
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
我应该怎样做才能使它保存的数据如下:
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
答案 0 :(得分:3)
1)我认为没有任何理由导入菜单我没有看到它做任何事情。
2)我认为你不需要在main.py
中导入显示尝试更改
file.write('\n'.join(list(i)))
到
file.write('\n{} {}'.format(i[0], i[1]))
这是我的输出
汤姆
A4纸(佳能):8.9 A4纸(彩虹):7.5长尺:0.85
短标尺:0.55
蓝笔:0.65
红笔0.65
黑色笔:0.65
2B铅笔:2.4
总计:22.149999999999995
答案 1 :(得分:0)
排序问题是由file.write(input('Customer name:'))
在最后缺少\n
引起的。需要file.write(input('Customer name:')+'\n')
。
然而,仅凭这一点不能为您提供所需的输出,因为从calculate1()
返回的列表并不包含数量和产品价格。虽然可以通过将一堆东西硬编码到其中来解决这个问题,但我认为对于更多的东西来说会更好......数据驱动"这将简化代码并使其更容易修改。
虽然我添加了一个名为PRICE_LIST
的新(有序)字典,其中包含产品名称和价格。其余代码使用它来尽可能多地控制它所执行的处理。
这就是我的意思(注意:所有代码都已放入单个文件中以简化更改):
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')
示例运行:
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
之后sample.txt
文件的内容:
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