Python01小购物车作业

本文介绍了一个简单的购物车程序实现过程,该程序允许用户基于其工资预算选择并购买商品。通过循环与条件判断,实现了商品购买及余额更新的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#__author:ocean
#Practice makes Perfect
#date: 2018/3/30
print("欢迎使用购物车程序.")
salary = 5000
list = ['iphone6s','mac book','coffee','python book','bicyle']
price= [5800,9000,32,80,1500]
list2=[]
price2=[]
flag = "f"
while flag !="Q" and flag!="q":
    msg='''
    ------------商店------------
    1. iphone6s     %s
    2. mac book     %s
    3. coffee       %s
    4. python book  %s
    5. bicyle       %s
    你的工资:        %s
    ----------------------------
    '''%(price[0],price[1],price[2],price[3],price[4],salary)
    print(msg)
    good=input("输入你要购买商品的序号:")
    if  good=="1":
        if(salary-price[0]>=0):
            print("已加入:",list[0],"到你的购物车,  当前余额:",salary - price[0],)
            salary=salary - price[0]
            list2.append(list[0])
            price2.append(price[0])
            flag=input("退出请按:(Q/q):")
        else:
            print("余额不足:",salary-price[0])
    elif    good=="2":
        if (salary - price[1] >= 0):
            print("已加入:", list[1], "到你的购物车,  当前余额:", salary - price[1], )
            list2.append(list[1])
            price2.append(price[1])
            salary = salary - price[1]
            flag = input("退出请按:(Q/q):")
        else:
            print("余额不足:", salary - price[1])
    elif    good=="3":
        if (salary - price[2] >= 0):
            print("已加入:", list[2], "到你的购物车,  当前余额:", salary - price[2], )
            list2.append(list[2])
            price2.append(price[2])
            salary = salary - price[2]
            flag = input("退出请按:(Q/q):")
        else:
            print("余额不足:", salary - price[2])
    elif    good=="4":
        if (salary - price[3] >= 0):
            print("已加入:", list[3], "到你的购物车,  当前余额:", salary - price[3], )
            list2.append(list[3])
            price2.append(price[3])
            salary = salary - price[3]
            flag = input("退出请按:(Q/q):")
        else:
            print("余额不足:", salary - price[3])
    elif    good=="5":
        if (salary - price[4] >= 0):
            print("已加入:", list[4], "到你的购物车,  当前余额:", salary - price[4], )
            list2.append(list[4])
            price2.append(price[4])
            salary = salary - price[4]
            flag = input("退出请按:(Q/q):")
        else:
            print("余额不足:", salary - price[4])
    else:
        print("输入错误!!!请输入1-5之间")
if(flag=="q" or "Q"):
    print("你已成功退出,以下是你购买的商品:")

    for i in range(len(list2)):
        print(list2[i],"    ",price2[i])


    print("余额:",salary)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值