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("\033[1;31m欢迎来到tongtong商场\033[0m".center(100,"*"))
    print("序列号" + "\t", "商品名" + "\t", "价格" + "\t", "数量" + "\t")
    for i,j in enumerate(goods_list):
        print(str(i+1)+"\t",goods_list[i]["name"]+"\t",str(goods_list[i]["price"])+"\t",str(goods_list[i]["number"])+"\t")

def login():
    sum=0
    global flag
    if flag==False:
        print("\033[1;33m请登录\033[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("\033[5;34m登录成功\033[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()

 

posted @ 2019-05-12 18:31  myfu  阅读(146)  评论(0编辑  收藏  举报