2016-11-13 3 views
1

ご協力いただきありがとうございます。私は、この形式で注文をプリントアウトしたい:正しい小計で領収書書式を印刷するプログラムを取得するにはどうすればよいですか?

85791008  Mango-1   £1  3 £3 
86139113 Strawberries-500g £1.50 2 £3 
Total cost of order:      £6 

これは私のコードです:

import csv 

option='yes' 
user_orders=[] 
final_price=0.00 

while option=='yes': 
    data=open('Product information.csv', 'rt') 
    purchase=csv.reader(data) 
    order=input('Please enter the GTIN-8 code of the product you would like to purchase: ') 
    for row in purchase: 
     for field in row: 
      if order in field: 
       quantity=int(input('How much of that item: ')) 
       final_price=quantity*float(row[2]) + final_price 
       receipt=('{} {} {} {} {}'.format(row[0]+'  ', row[1]+'  ', str(quantity)+'  ', "{:10.2f}".format(float(row[2]))+'  ', "{:10.2f}".format(quantity * float(row[2])))) 
       user_orders.append(receipt) 
       print('You have added '+(receipt)) 
       option=input('Would you like to add another iem to your order, yes or no: ') 
       if option=='no': 
        for user_order in user_orders: 
         print('\n' + user_order) 
        print('\nTotal cost of order:             '+ "{:10.2f}".format(final_price)) 

はどのようにして上部の形式でプリントアウトするために、これを編集するには?

+0

私の答えに表示されているように、最後の印刷ステートメントをループから移動します。 –

答えて

0

あなたのコードの主な問題は、ループ内でデフォルトの変数を宣言し、すべての繰り返し(final_price、user_orders)でリセットすることです。これとは別に、書式設定がうまくやられておらず、あなたはint型を価格に使用しました。これは良い考えではありません。 csvファイルの価格に浮動小数点が含まれているとエラーが発生しました。

import csv 

option='yes' 
user_orders=[] 
final_price=0.00 

while option=='yes': 
    data=open('Product information.csv', 'rt') 
    purchase=csv.reader(data) 
    order=input('Please enter the GTIN-8 code of the product you would like to purchase: ') 
    for row in purchase: 
     for field in row: 
      if order in field: 
       quantity=int(input('How much of that item: ')) 
       final_price=quantity*float(row[2]) + final_price 
       receipt=('{} {} {} {} {}'.format(row[0]+'  ', row[1]+'  ', str(quantity)+'  ', "{:10.2f}".format(float(row[2]))+'  ', "{:10.2f}".format(quantity * float(row[2])))) 
       user_orders.append(receipt) 
       print('You have added '+(receipt)) 
       option=input('Would you like to add another iem to your order, yes or no: ') 
       if option=='no': 
        for user_order in user_orders: 
         print('\n' + user_order) 
        print('\nTotal cost of order:             '+ "{:10.2f}".format(final_price)) 
+0

こんにちはNikolay、助けてくれてありがとうございますが、あなたのコードは最終小計を印刷しません。 : – Mario

+0

こんにちはNikolay、私は本当に助けていただきありがとうございます。上のオリジナルの質問を読んでください新しい問題を説明します – Mario

+0

Nikolay、soooたくさんありがとう!!万一、領収書の整列方法、すべての価格が調整されます。 – Mario

関連する問題