代码如下:
from operator import add
notice='''\
welcome to my shopping car
1 iphone 5800
2 book 100
3 bike 200
4 iphone8 8000\
'''
amount=raw_input('You amount in credict card')
items=[5800,100,200,8000]
name={'1':'iphone','2':'book','3':'bike','4':'iphone8'}
sum1=[0]
named=[]
print notice
while True:
choice=raw_input(">>>")
if choice.isdigit():
if ((reduce(add,sum1)+items[int(choice)-1])<int(amount)):
sum1.append(items[int(choice)-1])
named.append(name[choice])
print 'added [', name[choice], '] to your shopping'
else:
print 'please try another one'
if choice=='q':
print'====================================='
print 'goods price'
for i in range(1,len(sum1)):
print named[i-1],' ',sum1[i]
print 'sum:',reduce(add,sum1)
print 'balance',int(amount)-reduce(add,sum1)