이것은 내가하고있는 숙제를위한 것입니다.누적 합계 사전
다음과 같은 .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()