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()