冰淇淋销售报告文件(Python)

时间:2015-11-25 09:09:13

标签: python file

编辑11/25/2015

def main():
    outfile=open('icecreams.txt','w')
    sales_report1={'Vanilla':[8580.0,7201.25,8900.0]}
    sales_report2={'Chocolate':[10225.25,9025.0,9505.0]}
    sales_report3={'Rocky Road':[6700.1,5012.45,6011.0]}
    sales_report4={'Cookie Dough':[7901.25,4267.0,7056.5]}
    outfile.write(str(sales_report1)+'\n')
    outfile.write(str(sales_report2)+'\n')
    outfile.write(str(sales_report3)+'\n')
    outfile.write(str(sales_report4)+'\n')
    outfile.close()
    print('Data written to file') 
main()

#read list of icecreams
def main():
    infile=open('icecreams.txt','r')
    sales_report1=str(infile.readline())
    sales_report2=str(infile.readline())
    sales_report3=str(infile.readline())
    sales_report4=str(infile.readline())
    total=sales_report1
    infile.close()
    print(total)
    print(sales_report1,sales_report2,sales_report3,sales_report4)
main()

打印出来:

Data written to file
{'Vanilla': [8580.0, 7201.25, 8900.0]}

{'Vanilla': [8580.0, 7201.25, 8900.0]}
 {'Chocolate': [10225.25, 9025.0, 9505.0]}
 {'Rocky Road': [6700.1, 5012.45, 6011.0]}
 {'Cookie Dough': [7901.25, 4267.0, 7056.5]}

但是我需要将每个冰淇淋的销售额加起来,但是当我打印总数时,它会在sales_report1中打印所有内容,这是否意味着在这种情况下字典不是一个好主意?

0 个答案:

没有答案