美文网首页
2018-12-30 简单购物车程序

2018-12-30 简单购物车程序

作者: 化石0305 | 来源:发表于2018-12-30 00:19 被阅读0次
    def shopping_mall():
        shoppingmall = [
            ["汽      车",1000000],
            ["电      视",5000],
            ["冰      箱",3000],
            ["空      调",6000],
            ["笔记本电脑",8000],
            ["笔  记  本",200]
        ]
        return shoppingmall
    def input_saving ():
        while True:
            saving = input("请输入您的存款金额:")
            if saving.isdigit():
                saving = int(saving)
                return saving
                break
            else:
                print("您的输入不正确,请重新输入!")
    
    def input_choice():
        while True:
            choice = input("请输入您想购买的产品序号[q:退出]:")
            if choice.isdigit():
                choice = int(choice)
                if choice > 0 and choice < len(shoppingmall)+1:
                    return choice
                    break
                else:
                    print("您输入的序号不存在,请重新输入!")
            elif choice == "q":
                return choice
                break
            else:
                print("您的输入不正确,请重新输入!")
    def Processing_data(shoppingmall,saving):
        shopping_car = []
        he_ji = 0
        while True:
            for i,v in enumerate(shoppingmall,1):
                    print(i,v)
            choice = input_choice()
            if choice != "q":
                if saving > shoppingmall[choice-1][1]:
                    shopping_car.append(shoppingmall[choice-1])
                    saving -= int(shoppingmall[choice-1][1])
                    print("购买成功!您的余额为:%.2f元"%saving)
                else:
                    print("余额不足,请重新选择!")
            else:
                print('''您已购买如下商品:
    ------------------''')
                for i,v in enumerate(shopping_car,1):
                    he_ji = he_ji + int(v[1])
                    print(i,v[0],v[1])
                print('''------------------
    合计金额:%.2f元
    您的余额为:%.2f元。
    谢谢光临,欢迎下次再来!'''%(he_ji,saving))
                break
    
    if __name__ == "__main__":
        shoppingmall = shopping_mall()
        saving = input_saving()
        Processing_data(shoppingmall,saving)
    

    相关文章

      网友评论

          本文标题:2018-12-30 简单购物车程序

          本文链接:https://www.haomeiwen.com/subject/pdpmlqtx.html