购物车程序(函数版)

import os

good_list = {'咸鸭蛋': 50,
             'iPhone x': 7888,
             'mac book': 13888,
             '康师傅方便面': 5
             }
shopping_list = {}
cost = 0


def register():
    print('开始注册!')
    user_name = input('请输入用户名').strip()
    if os.path.exists('%s.txt' % user_name):
        print('该用户已注册,请登录!')
        login()
    else:
        while True:
            pwd = input('请输入密码')
            pwd_sure = input('请确认密码')
            if pwd == pwd_sure:
                print('注册成功,请登录!')
                with open(r'%s.txt' % user_name, 'w', encoding='utf-8') as f:
                    f.write('%s|%s' % (user_name, pwd))
                    f.flush()
                login()
                break
            else:
                print('两次密码不一致,请重新输入')
    pass


def login():
    print('开始登录!')
    user_name = input('请输入用户名').strip()
    if not os.path.exists('%s.txt' % user_name):
        print('该账户尚未注册,请先注册')
        register()
    else:
        while True:
            pwd = input('请输入密码')
            with open(r'%s.txt' % user_name, 'r', encoding='utf-8') as f:
                res = f.read().split('|')
            if pwd == res[1]:
                print('登录成功')
                break
            else:
                print('密码输入错误,请重新输入')
    return user_name


def shopping():
    global cost
    print('请先登录!')
    user_name = login()
    print('开始购物!')
    with open(r'%s.txt' % user_name, 'r', encoding='utf-8') as f:
        res = f.read().split('|')
    pwd = res[1]
    if len(res) < 3:
        print('余额不足,请充值!')
        mon = recharge(user_name)
    else:
        mon = int(res[2])
    while True:
        print('商品列表!')
        res = list(good_list)
        for k, v in enumerate(good_list, 1):
            print(k, v, '%s元' % good_list[v])
        buying = input('请输入你想购买的商品:').strip()
        if buying.isdigit():
            buying = int(buying)
            if buying > 0 and buying < len(good_list):
                if good_list[res[buying - 1]] < mon:
                    mon -= good_list[res[buying - 1]]
                    cost += good_list[res[buying - 1]]
                    if res[buying - 1] in shopping_list:
                        shopping_list[res[buying - 1]] += 1
                    else:
                        shopping_list[res[buying - 1]] = 1
                    quit_or_not = input('请问是否退出购物(Y/N)?')
                    if quit_or_not.lower() == 'y':
                        quit(mon, user_name,pwd)
                        break
                else:
                    print('余额不足请充值!')
                    charge_or_not = input('是否充值(Y/N)?').strip()
                    if charge_or_not.lower() == 'y':
                        mon = recharge(user_name)
                    else:
                        print('余额不足,结账退出!')
                        quit(mon, user_name, pwd)
                        break
                pass
            else:
                print('没有该商品!')
        else:
            print('请输入数字!')
    pass


def recharge(user_name):
    with open(r'%s.txt' % user_name, 'r', encoding='utf-8') as f:
        res = f.read().split('|')
    if len(res) < 3:
        mon = 0
    else:
        mon = int(res[2])
    pwd = res[1]
    while True:
        recharge_mon = input('请输入充值金额:')
        if recharge_mon.isdigit():
            recharge_mon = int(recharge_mon)
            mon += recharge_mon
            with open(r'%s.txt' % user_name, 'w', encoding='utf-8') as f:
                f.write('%s|%s|%s' % (user_name, pwd, mon))
                f.flush()
                break
        else:
            print('请输入数字!')
    return mon


def quit(mon, user_name, pwd):
    print('购物清单!')
    for i in shopping_list:
        print('购买商品%s\t单价%s\t数量%s\t'%(i,good_list[i],shopping_list[i]))
    print('总价:%s'%cost)
    with open(r'%s.txt'%user_name,'w',encoding='utf-8') as f:
        f.write('%s|%s|%s'%(user_name, pwd, mon))
        f.flush()
    pass


while True:
    choice_list = {1: register, 2: login, 3: shopping}
    choice = input('1、注册\n'
                   '2、登录\n'
                   '3、购物\n'
                   '请选择功能:')
    if choice.isdigit():
        choice = int(choice)
        if choice in [1, 2, 3]:
            if choice == 1:
                register()
            if choice == 2:
                login()
            if choice == 3:
                shopping()
            break
        else:
            print('没有该功能!')
    else:
        print('请输入数字!')

  

猜你喜欢

转载自www.cnblogs.com/DcentMan/p/11185563.html
0条评论
添加一条新回复