python写简单购物车_python 简单购物车小程序

salary=5000

goods_list=[['iphone6s',5800],['mac book',9000],['coffee',32],['python book',50],['bicycle',1500]]

print("Welcome to our store,the following is our goods _list")

shoping_cart=[]#购物车,进入购物车的商品是已购商品,如果要做未购商品的购物车,只需要加个变量salary_tmp

#打印商品列表

for i,j in enumerate(goods_list,1):

# print("%d. %-12s %d"%(i,j,k))

print("%d. %-12s %d"%(i,j[0],j[1]))

while True:

num=input("Please input the goods number you want to buy:")

#对输入进行检查,输入是quit退出,是商品号进行处理,是其他的报错

if num.isdigit():

num=int(num)-1

elif num=="quit":

break

else:

print("please input quit or the goods number")

continue

#获取商品价格和商品名称

goods_price=goods_list[num][1]

goods=goods_list[num][0]

#如果余额不足则无法购买,余额足够则让余额减少

if goods_price>salary :

print("Your money is not enough",salary-goods_price)

else:

print("the %s has already put into your shoping cart,your recent money: %d"%(goods,salary-goods_price))

shoping_cart.append([goods,goods_price])

salary -= goods_price

#已购商品提示

print("You have already bought %d goods"%(len(shoping_cart)))

for i in range(len(shoping_cart)):

print("%d. %-12s %d"%(i+1,shoping_cart[i][0],shoping_cart[i][1]))

print("Your recent money:%d\nLooking forward to your next visit"%salary)

Welcome to our store,the following is our goods _list

1. iphone6s 5800

2. mac book 9000

3. coffee 32

4. python book 50

5. bicycle 1500

Please input the goods number you want to buy:1

Your money is not enough -800

Please input the goods number you want to buy:2

Your money is not enough -4000

Please input the goods number you want to buy:3

the coffee has already put into your shoping cart,your recent money: 4968

Please input the goods number you want to buy:4

the python book has already put into your shoping cart,your recent money: 4918

Please input the goods number you want to buy:5

the bicycle has already put into your shoping cart,your recent money: 3418

Please input the goods number you want to buy:quit

You have already bought 3 goods

1. coffee 32

2. python book 50

3. bicycle 1500

Your recent money:3418

Looking forward to your next visit

来源:https://www.cnblogs.com/pqhuang/p/11195278.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值