python-练习4

来源:互联网 发布:阿里云国际版怎么注册 编辑:程序博客网 时间:2024/05/11 19:37

这里写图片描述

代码如下:

from operator import addnotice='''\welcome to my shopping car1 iphone 58002 book 1003 bike 2004 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 noticewhile 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)
原创粉丝点击