• python3 购物程序


    要求:

    一、启动程序后,选择是商家还是用户

    1、选择商家用户

    输入用户名,密码进入

    选择增加商品及价格:格式:  商品名称 价格

    选择编辑商品及价格:根据提示进行操作

    2、选择用户

    输入用户名,密码进入

    判断是否是第一次登录,如果是,提示进行充值

    充值完成,显示商品列表,输入商品名称进行购买,检测余额是否够,够就直接扣款,不够提示余额不足。

    3、可随时退出,退出时,打印已购买商品和余额。

    python 版本 3.5

    主函数:

    #Author by Andy
    #_*_ coding:utf-8 _*_
    import sys,time,os
    sys.path.append("E:my python studyday2")
    import login_new
    #定义显示商品列表函数
    def product_list():
        with open(r'E:my python studyfilesproduct_list.txt',encoding='utf-8') as f:
            for index,item in enumerate(f.readlines()):
                print(item,end='')
            print('
    ')
    
    #定义增加商品函数
    def add_product():
        while True:
            with open(r'E:my python studyfilesproduct_list.txt', 'a+', encoding='utf-8') as f:
                Product=input('请输入增加的商品名称及价格:')
                if Product=='q' or Product=='Q':
                    product_list()
                    exit()
                else:
                    f.write(Product)
                    f.write('
    ')
                    f.close()
                    product_list()
    #定义修改商品价格函数
    def edit_product():
        while True:
            f2 = open(r'E:my python studyfilesproduct_list.txt', 'r+', encoding='utf-8')
            with open(r'E:my python studyfilesproduct_list.txt','r+',encoding='utf-8') as f:
                print("您的商品列表:")
                product_list()
                old=input("请输入要修改的商品名称及价格:")
                if old == 'q' or old == 'Q':
                    print(product_list())
                    exit()
                else:
                    new=input("请输入修改后的商品名称及价格:")
                    if new == 'q' or new== 'Q':
                        print(product_list())
                        exit()
                    else:
                        for line in f:
                            if old in line:
                                line=line.replace(old,new)
                            f2.write(line)
                        print(product_list())
    #定义判断是否第一次登录及充值函数
    def chk_first_login():
        if os.path.exists(r"E:my python studyfilesalance.txt"):
            with open(r'E:my python studyfilesalance.txt', 'r', encoding='utf-8') as f:
                for Balance in f.readlines():
                    print('欢迎光临,您目前的余额为:', Balance)
        else:
            with open(r'E:my python studyfilesalance.txt', 'w+', encoding='utf-8') as f:
                print('欢迎光临,第一次使用,请先充值!')
                Balance = input('请输入充值金额:')
                f.write(Balance)
                print('正在充值,请稍候', end='')
                for i in range(10):
                    sys.stdout.write('.')
                    sys.stdout.flush()
                    time.sleep(1)
            print('
    充值成功!祝您购物愉快^_^')
    
    #定义获取价格函数
    def get_price(x):
        with open(r'E:my python studyfilesproduct_list.txt',encoding='utf-8') as f:
            item=[]
            price=[]
            for i in f.readlines():
                item.append(i.split()[0])
                price.append(i.split()[1])
            s=zip(item,price)
            d={}
            for k,v in s:
                d[k]=v
        if x not in d.keys():
            print("输入错误,请重新输入!")
            buy_main()
        else:
            return int(d[x])
    
    
    #定义获取余额函数
    def get_balance():
        with open(r'E:my python studyfilesalance.txt', 'r', encoding='utf-8') as f:
            balance=f.read()
        return int(balance)
    
    #定义购物车记录函数
    def shopping_record(record):
        with open(r'E:my python studyfilesshopping_cart.txt', 'a+', encoding='utf-8') as f:
            for i in record:
                f.write('%s
    '%(i))
    #定义获取购物记录函数
    def get_shopping_record():
        with open(r'E:my python studyfilesshopping_cart.txt', 'r', encoding='utf-8') as f:
            print(f.read())
    #定义购买函数
    def buy_main():
        choice=input("输入商品名称购买商品,退出请按q!:")
        if choice == 'q' or choice =='Q':
            print('我的购物车:',end='')
            get_shopping_record()
            print("欢迎下次光临,再见!")
            exit()
        else:
            Price = int(get_price(choice))
            print("您要购买的商品价格为:%s" %(Price))
            Balance=get_balance()
            if Price > Balance:
                print("33[31;1m对不起,您的余额不足!33[0m")
            else:
                print("您购买的%s 已添加至购物车"%(choice))
                Balance=str(Balance - Price)
                with open(r'E:my python studyfilesalance.txt', 'w+', encoding='utf-8') as f:
                    f.write(Balance)
                Shopping_cart=[]
                Shopping_cart.append(choice)
                shopping_record(Shopping_cart)
    #######################################################################################################################
    def fun_customers():
        login_name = input("Login:")
        login_pass = input("Password:")
        login_new.confirm(login_name,login_pass)
        chk_first_login()
        time.sleep(1)
        print("我们有一下商品供您选购:")
        product_list()
        while True:
            buy_main()
    def fun_seller():
        login_name = input("Login:")
        login_pass = input("Password:")
        login_new.confirm(login_name, login_pass)
        print("您的商品列表:")
        product_list()
        while True:
            print("增加商品输入a,修改商品价格输入)c:")
            choice=input(":")
            if choice=='a':
                add_product()
            elif choice=='c':
                edit_product()
            else:
                print("输入错误,请重新输入!")
    #####################################################################################################################
    
    print("商家入口输入s")
    print("用户入口输入c")
    role=input(":")
    while True:
        if role =='c' or role=='C':
            fun_customers()
        elif role == 's' or role == 'B':
            fun_seller()
        elif role == 'q' or role == 'Q':
            print("谢谢使用!")
        else:
            print('输入错误,请重新输入!')
    

      

    用户名密码判断函数:

    #Author by Andy
    #_*_ coding:utf-8 _*_
    #定义校验用户名及密码函数
    def confirm(x,y):
        login_name = x
        login_pass = y
        _username = open(r"E:my python studyfilesusername.txt")
        username_list = []
        for username in _username.readlines():
            username_list.append(username.strip("
    "))
            _username.close()
        _password = open(r"E:my python studyfilespassword.txt")
        password_list = []
        for passwd in _password.readlines():
            password_list.append(passwd.strip("
    "))
            _password.close()
        passwd_dict = {}
        s = zip(username_list, password_list)
        for k, v in s:
            passwd_dict[k] = v
        if passwd_dict[login_name] != login_pass:
            print ("username or password wrong!")
            return 1
        else:
    #        print ("Welcome!")
            return 0
    

      

      

  • 相关阅读:
    flexible.js 移动端自适应方案
    Vue为什么不能检测数组变动
    Vue 组件间通信六种方式
    训练首个神经网络:基本分类
    对seq2seq的粗浅认识
    数学模型的过拟合和欠拟合
    在二叉树中寻找值最大的节点并返回——LintCode入门
    Android 包管理机制
    自定义View的三种实现方式及自定义属性使用介绍
    Paint.setFlags中flag意义及使用方法
  • 原文地址:https://www.cnblogs.com/pythonstudy/p/6097334.html
Copyright © 2020-2023  润新知