zoukankan      html  css  js  c++  java
  • python-购物车

    goods_list=[{"name":"iphone xs","price":6299,"number":10,},{"name":"huawei","price"
    :3999,"number":5},{"name":"xiaomi","price":1999,"number":15}]
    shopping_cart=[]
    flag=False
    import os
    def good():
        print("33[1;31m欢迎来到tongtong商场33[0m".center(100,"*"))
        print("序列号" + "	", "商品名" + "	", "价格" + "	", "数量" + "	")
        for i,j in enumerate(goods_list):
            print(str(i+1)+"	",goods_list[i]["name"]+"	",str(goods_list[i]["price"])+"	",str(goods_list[i]["number"])+"	")
    
    def login():
        sum=0
        global flag
        if flag==False:
            print("33[1;33m请登录33[0m".center(100,"#"))
            count=0
            while count<3:
                uname=input("pls input username:").strip()
                pwd=input("pls input password:").strip()
                with open ("infor.txt",encoding="utf-8") as f1:
                    user,password,money=f1.readline().split("|")
                if uname==user and pwd==password:
                    print("33[5;34m登录成功33[0m")
                    flag=True
                    while True:
                        os.system("cls")
                        good()
                        use_select = input("请输入商品序号(q退出|c查看购物车|b结账付款):")
                        if use_select.isdigit():
                            if int(use_select) > len(goods_list):
                                print("输入有误,请重新输入!")
                                continue
                            if int(money)>=(sum+goods_list[int(use_select) - 1]["price"]):
                                goods_name = goods_list[int(use_select) - 1]["name"]
                                while True:
                                    nu = input("请输入购买数量:")
                                    if nu.isdigit():
                                        if int(nu) * goods_list[int(use_select) - 1]["price"] > int(money):
                                            print("您的余额不足,请重新选择")
                                            continue
                                        else:
                                            goods_list[int(use_select)-1]["number"]-=int(nu)
                                            bought = "商品名:%s,数量:%s" % (goods_name, nu)
                                            sum += int(nu) * goods_list[int(use_select) - 1]["price"]
                                            shopping_cart.append(bought)
                                            break
                            else:
                                print("余额不足,请从新选择!")
                                continue
                            continue
                        if use_select=="q":
                            flag=False
                            exit("谢谢光临!")
                        if use_select=="c":
                            if len(shopping_cart)==0:
                                print("您的购物车为空,请添加些商品吧!")
                                continue
                            else:
                                print(shopping_cart)
                                continue
                        if use_select=="b":
                            if len(shopping_cart)==0:
                                print("您的购物车为空,请添加些商品吧!")
                                continue
                            else:
                                b_s=input("确认结账(y确定|c取消)")
                                if b_s.lower()=="y":
                                    yue=int(money)-sum
                                    print("购买成功!")
                                    print("您的余额剩余%s元"%(yue))
                                    exit()
                                if b_s=="c":
                                    continue
                        else:
                            print("输入有误,请重新输入!")
                            continue
                else:
                    print("输入有误,您还有%s次机会"%(2-count))
                    count+=1
            print("您输入的输入次数超过3次")
            exit()
    login()
  • 相关阅读:
    pause
    vim 修改复制过来的代码缩进
    解决TeamViewer提示商业用途
    服务器上安装解决ole错误
    vs密匙
    oracle11g忘记管理员密码
    sql同时删除多个表的数据
    删除表中多余的重复记录(多个字段),只留有rowid最小的记录
    查找表中多余的重复记录(多个字段)
    删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
  • 原文地址:https://www.cnblogs.com/fumy/p/10853173.html
Copyright © 2011-2022 走看看