码迷,mamicode.com
首页 > 编程语言 > 详细

python之路2(作业)

时间:2016-01-14 23:47:53      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

购物车:当用户购买物品需要先输入预算,然后物品总价是否超出。

技术分享

###################购物车使用说明###################
#1:输入预算设定阀值。                              #
#2:进入第一次购买,选择物品序号及数量。            #
#3:继续购买或结算。                                #
#4:进入第二次购买,选择物品及数量。                #
#5:继续购买或结算                                    #
#6:进入第三次购买,选择物品及数量。                #
#7:结算退出程序。                                    #                            
#(注意当购买物品超出预算,会需要重新选择购买)        #
####################################################

__author__ = ‘Administrator‘
shop={ #定义物品字典
    ‘iphone6s‘:‘6500‘,
    ‘Apple MacBook Pro‘:‘10000‘,
    ‘Alienware‘:‘25000‘,
    ‘Apple iMac‘:‘17000‘,
}
a=‘欢迎开启购物之旅‘
b=‘购物列表‘
print (str.center(a,70,‘*‘,)) #显示语
#print(str.center(b,3,))
#for i,v in enumerate(shop.keys()):
#    print(i,v ,shop[v],‘$‘)
c=int(input(‘请输入需要购买预算:‘)) #定义用户预算
yusuan=print(‘您的预算为:‘,c,‘$‘) #显示
kai = True#定义循环条件
while kai:#无限循环
    print(str.center(b,3,)) #引导菜单
    for i,v in enumerate(shop.keys()): #加序号显示字典key和value
        print(i,v ,shop[v],‘$‘) #显示
    s1=int(input(‘请输入您购买商品的序号:‘)) #用户进入一级进行选择
    s2=int(input(‘请输入您购买商品数量:‘))#定义用户选择物品数量
    #count += 1
    for i,v in enumerate(shop.keys()):
        if i==s1:  #循环取值,如果用户输入序号与字典对应
            lei=v #定义用户选择物品的key
            lei1=int(shop[v]) #将value转换成整数
            zong1=lei1 * s2 #单价乘以用户输入的数量
            a1=[lei,lei1,s2,zong1]#将用户第一次购买的物品放入一个列表中
            print(a1)#打印列表
            b1=input(‘继续购买请按任意键,结算购买请按q:‘) #提取用户下一步指令
            if zong1 > c:#判断是否大于预设值
                print(‘对不起您选取商品超出预算,请重新选取‘)
                break#大于预算则重新选择物品
            if b1 == ‘q‘: #如果用户输入Q则结算
                x="类型   单价   数量   总价"
                print(x)
                print(a1)
                #print(a1[lei],a1[lei1],a1[s2],a1[zong1])
                y1 = c - zong1
                print (‘消费:‘,zong1)
                print (‘预算余额:‘,y1)
                print (‘结算成功‘)
                kai=False
            else:
                yue=c - zong1 #显示预算剩余
                print(‘预算余额:‘,yue)#继续第二次购买
                for i1,v1 in enumerate(shop.keys()):
                    print(i1,v1 ,shop[v1],‘$‘)
                s3=int(input(‘请输入您购买商品的序号:‘))
                s4=int(input(‘请输入您购买商品数量:‘))

        #count += 1
                for i1,v1 in enumerate(shop.keys()):#以序号显示字典
                    if i1==s3:#取用户输入物品的key
                        lei2=v1
                        lei3=int(shop[v1])
                        zong2 = lei3 * s4 #本次购买的总价
                        a2=[lei2,lei3,s4,zong2]#第二次购买物品赋值
                        print(a2)
                        b2=input(‘继续购买请按任意键,结算购买请按q‘)#剩下的请参考以上
                        if zong2 > yue:
                            print(‘对不起您选取商品超出预算,请重新选取‘)
                            break
                        if b2 ==‘q‘:
                            x="类型 单价 数量 总价"
                            print(x)
                            print(a1)
                            print(a2)
                            y1 = yue - zong2
                            feiyong2=zong1 + zong2
                            print(‘本次消费:‘,feiyong2,)
                            print (‘预算余额:‘,y1)
                            print (‘结算成功‘)
                            kai=False
                        else:
                            yue1=zong1 + zong2
                            yue2=c - yue1
                            print (‘预算余额:‘,yue2)
                            for i3,v3 in enumerate(shop.keys()):
                                print(i3,v3 ,shop[v3],‘$‘)
                            s5=int(input(‘请输入您购买商品的序号:‘))
                            s6=int(input(‘请输入您购买商品数量:‘))
                            #count += 1
                            for i4,v4 in enumerate(shop.keys()):
                                if i4==s5:
                                    lei4=v4
                                    lei5=int(shop[v4])
                                    zong3=lei5 * s6
                                    a4=[lei4,lei5,s6,zong3]
                                    print(a4)
                                    b3=input(‘结算购买请按q‘)
                                    if zong3>yue2:
                                        print(‘对不起您选取商品超出预算,请重新选取‘)
                                        break
                                    if b3 == ‘q‘:
                                        x="类型 单价 数量 总价"
                                        print(x)
                                        print(a1)
                                        print(a2)
                                        print(a4)
                                        y3 = yue2 - zong3
                                        feiyong3=zong3 + zong2 +zong1
                                        print(‘本次消费:‘,feiyong3,)
                                        print (‘预算余额:‘,y3)
                                        print (‘结算成功‘)
                                        kai=False

python之路2(作业)

标签:

原文地址:http://www.cnblogs.com/dahuige/p/5131800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!