• 用函数模拟简单的购物车(Python)


    """
    购物车功能:
    	a.引导用户输入金额
    	b.给用户展示所有的商品
    	c.引导用户输入需要进行的操作【添加  删除 结算购物车  退出】
    	d.引导用户选择商品
    	e.引导用户输入需要购买的商品数量
    	f.添加到购物车【容器】
    	g.整个循环的操作,循环的次数不确定
    
    """
    
    def add():
        print(super_market)  # 超市商品列表
        print(price_list)              #商品价格表
        commodity = input("please input you need to add to your shopping basket:")          #请输入你需要添加的商品
        commodity_number = int(input("please input the number of commodity:"))              #请输入你需要添加的商品数量
        user_market[commodity] = commodity_number                                           #将商品加到用户购物车中
        super_market[commodity] -= commodity_number
        return
    def dell():
         dell_name = input("please input you del name :")  # 移除的商品名称
         dell_number = int(input("please the number you del:"))                              #移除的商品的数量
         user_market[dell_name] -=dell_number                                                #将购物车中的商品移除
         super_market[dell_name] += dell_number                                              #商品放回后超市现有的商品数目
         print(user_market)
         return
    def looking():
        print(super_market)
        print(user_market)
        return
    def settlement():
         amout = 0
         print(user_market)
         for key in user_market:
             amout += user_market[key] * price_list[key]
         print(amout)
         if amout > money_user:                              #如果总价高于所带的金额,退出循环
             print("you money is not pay for,please take the full money!")
         return False
    def exit():
        print("Thank you for your presence and welcome to come again next time!!")
        return False
    money_user = int(input("please user input your take money:"))
    super_market = {"apple":50,"watermelon":20,"nuddles":40,"drinks":30}
    price_list = {"apple":2,"watermelon":10,"nuddles":6,"drinks":3}
    user_market = {}
    while True:
        operation = int(input("please input your operation
    (1.add****2.del****3.looking****4.settlement****5.exit) :"))
        if operation == 1:
            add()
        elif operation ==2:
            dell()
        elif operation ==3:
            looking()
        elif operation ==4:
            settlement()
            if settlement() == False:
                break
        elif operation ==5:
            exit()
            if exit() == False:
                break
    

      

  • 相关阅读:
    【12c-多租户篇】Oracle 12c体系结构之多租户
    【12c-建库篇】Oracle 12c利用Create database手工创建数据库
    【12c-建库篇】Oracle 12c利用DBCA创建数据库
    【12c-安装篇】Oracle 12c软件安装
    redis单机安装
    MongoDB内置角色详解(自官方文档)
    MongoDB3.4创建用户
    MongoDB副本集常用管理命令
    使用keyfile安全认证搭建MongoDB副本集
    MongoDB参数详解之enableLocalhostAuthBypass
  • 原文地址:https://www.cnblogs.com/lcc1234/p/11285047.html
Copyright © 2020-2023  润新知