2017-11-24 19 views
0

私はクラスの簡単な食料品リストに取り組んでいます。最初のステップでは、ユーザーからの入力で空の辞書とリストを作成します。すべての食料品は辞書(grocery_item {})に入り、すべての辞書がリスト(grocery_history)に追加されます。辞書の値が「定義されていません」Python

私のスクリプトは、現在、次のようになります。

grocery_item = {} 

grocery_history = [] 

stop = 'go' 

while stop == 'go' or stop == 'c': 
    item_name = input("Item name: \n") 
    quantitiy = int(input("Quantitiy purchased:\n")) 
    cost = float(input("Price per item:\n")) 
    grocery_item.update({'name' : item_name, 'number' : quantitiy, 'price' : 
         cost}) 
    grocery_history.append(grocery_item) 
    stop = input("Would you like to enter another item?\n Type 'c' for continue 
       or 'q' to quite:\n") 

私はこの時点でgrocery_history印刷する場合、それは私が意図したとおりに辞書のリストを出力します。食料品リストの次のステップでは、アイテムの総計を見つけようとしています。しかし、各項目の個々の値を見つけようとするときに、forループを使用してリスト内の各辞書にアクセスすると、その食料品の辞書エントリを表示したにもかかわらず、キーが定義されていないというエラーが表示されます。のキーの値がありました。

このセクションのための私のスクリプトは次のようになります。

grand_total = 0 

for item in grocery_history: 
    I = 0 
    for price in item: 
     item_total = number * price 
     grand_total += item_total 
     print(number + name + '@' + '$' + price + 'ea' + '$' + round(item_total,2)) 
     item_total = 0 
     I += 1 

print('$' + round(grand_total,2)) 

それは私がキーのいずれかを使用しようと最初の行であるため、エラーが、私はITEM_TOTALを見つけようとしていますしたラインのためであります。私はgrocery_item(数字)/ grocery_item(価格)としてキーを書き直そうとしましたが、同じエラーメッセージが表示されます。

私は何か助けていただきありがとうございます!

+1

これはPython辞書の仕組みではありません。 – anonymoose

+1

こんにちは!あなたの2番目のセクションでは、各 'item'は辞書(食料雑貨品)です、いいえ?もしそうなら、2番目の 'for'ループは必要ありません。 item ['price'] 'でアイテムの価格にアクセスします。 –

+0

'item'を反復処理しないでください。例えば、' item ['price'] 'や' item ['name'] 'などに直接アクセスしてください。 –

答えて

0

このような何か試してください:あなたは辞書のキーをループにする必要があります辞書にforループを使用したい場合は

grocery_history = [] 
stop = 'go' 
while stop == 'go' or stop == 'c': 
    grocery_item = {} 
    grocery_item['name'] = input("Item name: \n") 
    grocery_item['number'] = int(input("Quantitiy purchased:\n")) 
    grocery_item['price'] = float(input("Price per item:\n"))  
    grocery_history.append(grocery_item) 
    stop = input("Would you like to enter another item?\n Type 'c' for continue or 'q' to quit:\n") 

grand_total = 0 
for item in grocery_history: 
    item_total = 0 
    item_total = item['number'] * item['price'] 
    grand_total += item_total 
    print(item['number'], item['name'], '@', '$', item['price'], 'ea', '$', round(item_total,2)) 
print('$', round(grand_total,2)) 

を。あなたは.keys()または.items()のdictの関数を使ってそれを行うことができます。しかし、上記のように、コード内のdictキーをループする必要はありません。

0
grocery_item = {} 

grocery_history = [] 

stop = 'go' 

while stop == 'go' or stop == 'c': 
    item_name = input("Item name: \n") 
    quantitiy = int(input("Quantitiy purchased:\n")) 
    cost = float(input("Price per item:\n")) 
    grocery_item.update({'name' : item_name, 'number' : quantitiy, 'price' : 
         cost}) 
    grocery_history.append(grocery_item) 
    stop = input("Would you like to enter another item?\n Type 'c' for continue or 'q' to quit:\n") 


grand_total = 0 
for item in grocery_history: 
    item_total = item['number'] * item['price'] 
    grand_total += float(item_total) 
    print(str(item['number']) + " " + str(item['name']) + ' @ ' + '$' + str(item['price']) + 'ea' + "\n" + '$' + str(round(item_total,2))) 
    item_total = 0 
print("\n" + '$' + str(round(grand_total,2)) + " is the grand total.") 
+0

21行目のstr()メソッドが欠落していました。私はまた、 "$"を "@"に連結して美しい目的のためのスペースを与えています。私はこれがあなたが探しているものであることを願っています。 –

+0

編集ボタンで自分の回答を編集できます。それはあなたの答えにコメントを追加するよりも優れています。 – MikaS

関連する問題