これは私がやっている宿題です。実行中の辞書を含む辞書
私はこのような.txtファイルを持っています。私がやろうとしている何
11
eggs
1.17
milk
3.54
bread
1.50
coffee
3.57
sugar
1.07
flour
1.37
apple
.33
cheese
4.43
orange
.37
bananas
.53
potato
.19
は、あなたが単語「卵」で、それは両方のコストを追加し、「EXIT」になるまで続ける必要があり、その後の単語「パン」を入力すると、実行中の合計を維持ありますまた私は 'KeyError'に遭遇し、そのことについても助けが必要です。
def main():
key = ''
infile = open('shoppinglist.txt', 'r')
total = 0
count = infile.readline()
grocery = ''
groceries = {}
print('This program keeps a running total of your shopping list.')
print('Use \'EXIT\' to exit.')
while grocery != 'EXIT':
grocery = input('Enter an item: ')
for line in infile:
line = line.strip()
if key == '':
key = line
else:
groceries[key] = line
key = ''
print ('Your current total is $'+ groceries[grocery])
main()