1
这是我正在做的功课。总字典
我有一个看起来像这样的.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
我试图做的是保持运行总计,当您在Word类型“鸡蛋”,那么单词“面包”,它需要同时添加的成本,并继续下去,直到“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()