温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python购物车实现

发布时间:2020-06-24 09:06:40 来源:网络 阅读:386 作者:zjy1002261870 栏目:编程语言

salary=int(input("please input your salary:"))
product_list=[['iphone',5299],['coffee',30],['bike',299],['vivo x9',2499],['cake',40],['book',99]]
product_car={}
total_cost=0

while True:
print('--------可以购买的商品如下--------')
for number in range(len(product_list)):
product = product_list[number]
print(number,product)
print('q','quit')
choice=input('input your select product number or q').strip()
if choice.isdigit():
choice=int(choice)
if choice < len(product_list) and choice >=0:
product = product_list[choice] #获取到要购买的商品信息和价格
if salary-product[1]>=0: #判断是否买得起
salary -=product[1]
print('将商品%s加入购物车,你现在的余额是%s'%(product[0],salary))
if product[0] in product_car:
product_car[product[0]][1]+=1 #商品已在购物车,将商品数量加1
else:
product_car[product[0]]=[product[1],1] #商品未在购物车,将商品单价和数量加入购物车
print('目前购物车',product_car)

        else:
            print('你买该商品%s,还差%s元'%(product[0],product[1]-salary))

    else:
        print('没有你选择的商品')
elif choice == 'q':
    print('您购买的商品信息如下')
    print('id\t商品\t数量\t单价\t总价')
    icount = 1
    for key in product_car:
        total_cost+= product_car[key][0]*product_car[key][1]
        print('%s\t%s\t\t%s\t%s\t%s'%(icount,key,product_car[key][1],product_car[key][0],product_car[key][0]*product_car[key][1]))
        icount+=1
    print('您的总消费为%s'%total_cost)

    break
else:
    print('大哥,您输入有误吧')
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI