用python制作购物车小程序

1.列表与元组组合的形式存放商品

goods=[
    ('ipone',6999),
    ('macpro',12000),
    ('bike',4000),
    ('book',40),
    ('clothe',200),
    ('house',1000000)]

2.用户输入自己薪资后显示商品列表

salary=input('Please input your salary:')
salary=int(salary)
for index,item in enumerate(goods):
    print(index+1,item)

3.初始化购物车为空,建立while循环:每次买完后还可以继续挑选

shopping_list=[]
while(True):
    user_choice=input('Please select your choice:')
    #用户选择判断,是否输入的是数字,如果是再判断是否超出价格
    if user_choice.isdigit():
        user_choice=int(user_choice)
        if user_choice<=len(goods) and user_choice>0:
        #再判断价格
            if(salary>=goods[user_choice-1][1]):
                shopping_list.append(goods[user_choice-1])
                salary-=goods[user_choice-1][1]
                print("you have bought the %s into your shopping  cart,your current rest is %s!"%(goods[user_choice-1][0],salary))
                continue
            else:
                print("your current balance is %s,cannot pay for it"%(salary))
        else:
            print("invalid option,please re-selected your goods!")

    #如果输入不是数字再判断输入是否为q
    elif user_choice=='q':
            if(len(shopping_list)==0):
                print("your shopping cart is empty!")
                exit()
            else:
                print("----Following is your shopping cart---")
                for item,good in enumerate(shopping_list):
                    print(item+1,good)
                print("your current balence is %s"%(salary))
                exit()
    else:
        print("invalid option!")
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值