购物车终极函数版

Posted sw-z

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车终极函数版相关的知识,希望对你有一定的参考价值。

def waicen():
    while tag:
        print(1.注册
2.登录
3.退出)
        user_choose = input(请输入操作的编号>>:).strip()
        if user_choose == 1:
            register()
            continue
        if user_choose == 2:
            login()
            break
            continue
        if user_choose == 3:
            exit()
        else:
            print(请输入正确的编号)


def neicen():
    print(1.购物
2.充值
3.返回)
    user_choose3 = input(【请选择操作编号:)
    if user_choose3 == 1:
        shopping()
    elif user_choose3 == 2:
        recharge()
    elif user_choose3 == 3:
        waicen()


def open_file():
    global yedic
    with open(ye.txt) as ye_f:
        for data1 in ye_f:
            yedic = {}
            user_ye = data1.split(,)
            yedic[user_ye[0]] = int(user_ye[1].strip(
))


def register():
    l1 = []
    name1 = input(【请输入要注册的用户名:).strip()
    with open(db.txt) as f:
        for i in f:
            l1.append(i.split(,))
    if name1 in l1:
        print(输入的用户名已被注册!)
    else:
        password1 = input(【请输入密码:)
        if   in list(password1):
            print(密码中不能有空格)
        else:
            with open(db.txt, a) as f:
                f.write(%s,%s
 % (name1, password1))

            with open(ye.txt, a) as zj_f:
                zj_f.write(%s,0
 % name1)
            print(注册成功)
            waicen()


def login():
    global count
    count = 0
    while True:
        l2 = []
        global name2
        name2 = input(【请输入用户名:).strip()
        password2 = input(【请输入密码:)
        with open(db.txt, r) as f:
            data = f.read().split(
)
            for i in data:
                l2.append(i.split(,))
        if [name2, password2] not in l2:
            print(用户名或密码错误)
            count += 1
        if count == 3:
            print(错误三次程序退出)
            exit()
        if [name2, password2] in l2:
            print(登录成功,尽情购物吧!)
            neicen()


def shopping():
    global zj2, sy
    zj2 = 0
    zj = 0
    zj1 = 0
    for k, v in msg_dic.items():
        print(商品名:%s   单价%s % (k, v))
    open_file()
    balance = yedic[name2]
    print(余额:%s.center(20, *) % balance)
    while True:
        sy = int(balance) - zj2
        if sy < 0:
            print(余额不足,请充值)
            user_choose4 = input(请选择1.充值2.退出)
            if user_choose4 == 1:
                with open(ye.txt, w) as rep_f:
                    yedic[name2] = int(yedic[name2]) - sy
                    for k, v in yedic.items():
                        rep_f.write(%s,%s
 % (k, v))
                recharge()
            if user_choose4 == 2:
                waicen()
        goods = input(【请输入要购买的商品名或输入 1 返回主菜单:).strip()
        if goods == 1:
            neicen()
        elif goods not in msg_dic:
            print(商品不存在)
            continue
        if goods in msg_dic and goods not in order:
            while True:
                num = input(【请输入要购买的数量:).strip()
                if num.isdigit() is True:
                    shopping_cart.append([goods, msg_dic[goods], num, msg_dic[goods] * int(num)])
                    print(购物车:)
                    for goods_line in shopping_cart:
                        print(商品名:{n} 单价{d} 数量{s} 总价{z}.format(n=goods_line[0], d=goods_line[1], s=goods_line[2],
                                                                 z=goods_line[3]))
                    zj2 += goods_line[3]
                    order.append(goods_line[0])
                    order_number = order.index(goods)
                    print(购买的所有商品总价格为¥%s % zj2)
                    break

                else:
                    print(请确认输入的是数字)


        elif goods in order:
            while True:
                num1 = input(1【请输入要购买的个数:).strip()
                order_number = order.index(goods)
                if num1.isdigit() is True:
                    num2 = shopping_cart[order_number][2]
                    total = shopping_cart[order_number][3]
                    shopping_cart[order_number][2] = int(num2) + int(num1)
                    shopping_cart[order_number][3] = int(total) + int(num1) * msg_dic[goods]
                    print(购物车:)
                    for i in shopping_cart:
                        print(商品名:{name} 单价:{price} 数量:{number} 总价:{total}.format(name=i[0], price=i[1], number=i[2],
                                                                                    total=i[3]))
                    zj2 += i[3]
                    print(购买的所有商品总价格为¥%s % zj2)
                    break
                elif num1.isdigit() is False:
                    print(请确认输入的是数字。)

        user_choose1 = input(【是否付款(是否)).strip()
        if user_choose1 == :
            pay()

        else:
            print(继续购物!)


def pay():
    open_file()
    if int(yedic[name2]) < int(zj2):
        print(余额不足,请选择操作
1.充值
2.退出)
        user_choose5 == input(>>:)
        if user_choose5 == 1:
            recharge()
        if user_choose5 == 2:
            waicen()
    elif int(yedic[name2]) > int(zj2):
        yedic[name2] -= zj2
        with open(ye.txt,w) as zf_f:
            for k, v in yedic.items():
                zf_f.write(%s,%s
 % (k, v))
        print(支付成功)
        open_file()
        print(余额还剩%s%yedic[name2])
        neicen()








def recharge():
    while True:
        balance1 = input(请输入要充值的金额:).strip()


        if balance1.isdigit() is True:
            with open(ye.txt) as ye_f:
                for data1 in ye_f:
                    yedic = {}
                    user_ye = data1.split(,)
                    yedic[user_ye[0]] = user_ye[1].strip(
)
            with open(ye.txt,w) as rep_f:
                yedic[name2] = int(balance1) + int(yedic[name2])
                for k,v in yedic.items():
                    rep_f.write(%s,%s
%(k,v))
            print(【充值成功,现余额为¥%s%yedic[name2])

            user_choose2 = input(1.继续充值
2.返回购物
3.返回主菜单
>>:).strip()
            if user_choose2 == 1:
                continue
            if user_choose2 == 2:
                shopping()
            if user_choose2 == 3:
                neicen()
            else:
                print(确认输入无误)









#
global tag,balance,msg_dic,order

msg_dic = {
    苹果: 10,
    特斯拉: 100000,
    mac: 3000,
    lenovo: 30000,
    鸡肉: 10,
    iphonex:5800,
    咖啡:30,
    vivo:2499,
    自行车:200

}

f=open(db.txt,a)
f.close()
f= open(ye.txt,a)
f.close()
f=open(gwc.txt,a)
f.close()

shopping_cart = []
order = []
order_number = 0
tag = True
balance = 0




waicen()

 

以上是关于购物车终极函数版的主要内容,如果未能解决你的问题,请参考以下文章

html 基本终极版代码

PHP MYSQL 生成JSON代码 终极版

终极购物车

PHP基础终极版-面试大全 深度理解变量的传值和引用

angular js 终极购物车(转)

阿里正式发布《Java开发手册》终极版!