python3 练习题(购物车)

'''
购物车程序
需求:
1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户根据商品编号购买商品
3.用户选择商品后,检查余额是否够,够就直接扣款,不够就提醒
4.用户可一直购买商品,也可随时退出,退出时,打印已购买商品和余额
'''
#商品列表
products_list = [['Iphone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 80], ['Nike Shoes', 799]]

print("欢迎来到中关村手机城!".center(50, "*"))
#充值
while True:
    salary = input("请输入您的工资: ").strip()
    if salary.isdigit():
        salary = int(salary)
        print("恭喜您充值成功! 您的账号余额为: %d元." % salary)
        break
    else:
        print("输入有误! 请重新输入您正确的工资.")

#总消费金额
consume_total = 0
#购买商品
shopping_cart = {}
while True:
    #显示商品信息
    print("中关村手机城商品信息".center(50, "*"))
    print("商品序号	商品名称	商品价格")
    for ind, products in enumerate(products_list):
        print("%d			%s		%d" % (ind, products[0], products[1]))
    print("Q			退出")

    #用户选择商品编号
    user_choice = input("请输入您要购买的商品编号: ").strip()
    if user_choice.isdigit():
        user_choice = int(user_choice)
        #添加购物车并结算
        if user_choice >= 0 and user_choice < len(products_list):
            #余额足
            if salary - consume_total - products_list[user_choice][1] >= 0:
                shopping_name = products_list[user_choice][0]
                shopping_price = products_list[user_choice][1]
                #判断商品是否已买过
                if user_choice in shopping_cart:
                    shopping_cart[user_choice][2] += 1
                else:
                    shopping_cart[user_choice] = [shopping_name, shopping_price, 1]
                consume_total += shopping_price
                print("恭喜您!成功购买商品: %s,本次消费: %d元. 总消费: %d元. 账号余额: %d元." % (shopping_name, shopping_price, consume_total, salary - consume_total))
            #余额不足
            else:
                print("余额不足! 已消费: %d元. 账号余额: %d元." % (consume_total, salary - consume_total))
        else:
            print("商品不存在! 请重新选择.")
    elif user_choice.upper() == "Q":
        break
    else:
        print("商品不存在! 请重新选择.")

#判断购物车是否为空
if shopping_cart != {}:
    print("您已购买的商品信息".center(50, "*"))
    print("商品编号	商品名称	商品单价	商品数量")
    for id, cars in shopping_cart.items():
        print("%d			%s		%d			%d" % (id, cars[0], cars[1], cars[2]))
    print("总消费: %d元, 账号余额: %d 元" % (consume_total, salary - consume_total))

print("欢迎下次光临!".center(50, "*"))

------------------------------------------------------------购物车----------------------------------------------------------

'''
购物车
1, 用户先给自己的账号充钱: 比如先充3000元.
2, 页面显示 序号 + 商品名称 + 商品价格, 如:
        1   电脑  1999
        2   鼠标  10
        ...
        n 购物车结算
3,  用户输入选择的商品序号,然后打印商品名称及商品价格,并将此商品,添加到购物车,用户还可继续添加商品.
4, 如果用户输入的商品序号有误, 则提示输入有误, 并重新输入.
5, 用户输入n为购物车结算, 依次显示用户购物车里面的商品, 数量及单价, 若充值的钱数不足, 则让用户删除某商品,
    直至可以购买, 若充值的钱数充足, 则可以直接购买.
6, 用户输入Q或者q退出程序.
7, 退出程序之后, 依次显示用户购买的商品, 数量, 单价, 以及此次消费多少钱, 账号余额多少.
'''
products_list = [("电脑", 1999), ("鼠标", 10), ("键盘", 30), ("音响", 888), ("耳机", 66)]
shopping_cart = {}
print("--------------------欢迎来到北京中关村电脑城!--------------------")

#用户充值
while True:
    money = input("请输入您要充值的金额: ").strip()
    if money.isdigit():
        money = int(money)
        print("恭喜您,成功充值: %d元. 账号余额为: %d元!"%(money, money))
        break
    else:
        print("输入有误,请重新输入")

Flag = True
while Flag:
    # 显示商品
    print("商品信息列表".center(50, "-"))
    print("序号	商品名称	商品价格")
    for ind, products in enumerate(products_list):
        print("%d		%s		%d" % (ind, products[0], products[1]))
    print("n		购物车结算")

    #提示用户输入
    user_choice = input("请输入您要购买的商品序号(退出请输入Q或q): ").strip()
    if user_choice.isdigit():
        user_choice = int(user_choice)
        #添加购物车
        if user_choice >= 0 and user_choice < len(products_list):
            cart_list = products_list[user_choice]
            if user_choice in shopping_cart:
                shopping_cart[user_choice][2] += 1
            else:
                shopping_cart[user_choice] = [cart_list[0], cart_list[1], 1]
            print("商品: %s 单价: %d元,已成功添加到您的购物车!"%(cart_list[0], cart_list[1]))
        #商品序号输入错误
        else:
            print("输入有误,请重新正确的商品序号")
    #用户结算
    elif user_choice.upper() == "N":
        while True:
            #判断购物车内是否有商品
            if shopping_cart == {}:
                print("无法结算,购物车现在空空如也,请选择您要购买的商品序号!")
                break

            #显示用户购物车里面的商品
            consume = 0
            print("您购物车内的商品信息".center(50, "-"))
            print("商品序号	商品名称	商品数量	商品单价")
            for ind, cars in shopping_cart.items():
                print("%d			%s		%d			%d"%(ind, cars[0], cars[2], cars[1]))
                consume += cars[1]*cars[2]
            print("商品总价: %s"%consume)

            #账户余额足
            if money - consume >= 0:
                print("恭喜您购买成功! 此次共消费: %d元, 账号余额: %d元."%(consume, money-consume))
                Flag = False
                break
            #账号余额不足
            else:
                del_choice = input("账号余额不足,你还缺%d元,请选择您要移除购物车内的商品序号: "%(consume-money)).strip()
                if del_choice.isdigit():
                    del_choice = int(del_choice)
                    if del_choice in shopping_cart:
                        del_name = shopping_cart[del_choice][0]
                        shopping_cart[del_choice][2] -= 1
                        if shopping_cart[del_choice][2] == 0:
                            del shopping_cart[del_choice]
                        print("成功从购物车移除一件商品: %s!"%del_name)
                    else:
                        print("输入有误,请重新输入要是移除的商品序号!")
                else:
                    print("输入有误,请重新输入要是移除的商品序号!")
    #退出
    elif user_choice.upper() == "Q":
        break
    else:
        print("输入有误,请重新输入")
print("欢迎下次光临!".center(50, "-"))

 

 
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10681670.html