Python小练习之购物车

版权声明:如需转载,注明出处。 https://blog.csdn.net/qq_22510521/article/details/79944794

购物车

---

要求

1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表


2、允许用户根据商品编号购买商品


3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 


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


5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示


6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买


7、允许查询之前的消费记录


main.py

import judgment_user


def main():
    with open("user.txt","r",encoding="utf-8") as f:#获取user.txt内容
        user_info = f.read() # 显示文本内容
        #print(user_info)
        user_list = user_info.split() # 加入列表
        #print(user_list)
        user_dic = {} # 定义空字典
        for itme in user_list: # 循环user_list列表
            user_list = itme.split("-") # 截取-
            #print(user_list)
            user_dic[user_list[0]] = user_list[1] # 把文本中的内容截取----后加入字典
            #print(user_dic)
    count = 0 # count从0开始
    while count < 3:
        username = input("\033[38;1m请输入用户名-->>\033[0m:")
        if username in user_dic: #判断username是否在字典中
            #r+为读写模式
            with open("lock.txt", "r+", encoding="utf-8") as f:
                lock_info = f.read()
                lock_list = lock_info.split() #截取文本中的空格
                if username in lock_list:  # 判断输入的用户是否在被锁定文件中
                    print("\033[31;1m此用户被锁定,请联系管理员解绑\033[0m")
                else:
                    password = input("\033[38;1m请输入密码-->>\033[0m:")
                    #print(user_dic)
                    if password == user_dic[username]:#判断字典中帐号密码是否正确
                        print("%s:\033[36;1m登录成功!\033[0m"%username)
                        judgment_user.mkdir(username)
                        exit()
                    else:
                        count +=1 #密码每次错误数量加1
                        if count==3: #错误3次帐号锁定
                            f.write('\n'+username)
                            print("\033[31;1m密码错误3次,帐号已被锁定\033[0m")
                        else:
                            print("\033[31;1m密码错误,还可以再试%s次\033[0m"%(3-count))
        else:
            print("\033[31;1m用户名输入错误!\033[0m")

if __name__ == '__main__':
    main()

judgment_user.py

import shoppingcart

def mkdir(path):
    '判断是否是第一次登录'
    # 引入模块
    import os

    # 去除首位空格
#    path = path.strip()
    # 去除尾部 \ 符号
#    path = path.rstrip("\\")

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        print(path + ' 用户第一次登陆')
        salary = input("请输入工资:")
        # 创建目录操作函数
        os.makedirs(path)
        shoppingcart.shoppingcart(path, salary)
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path + ' 用户已登陆过')
        choose = input("\033[36;1m 是否查询之前消费记录(查询请输入y,不查询任意键)-->> \033[0m:")
        shoppingcart.Query_the_records(choose, path)
        with open('./' + path + "/salary.txt", "r+", encoding="utf-8") as f:
            salary = f.read()
        print("余额剩余:", salary)
        shoppingcart.shoppingcart(path, salary)
        return False

shopppingcart.py

#本模块包换购物车主体函数、购物记录查询函数


def shoppingcart(username,salary):
    ' 购物车主体函数 '

#    salary = input("请输入工资:")

    product_list = [
        ('Iphone',5800),
        ('mac',9800),
        ('bike',800),
        ('watch',5800),
        ('coffee',31),
        ('alex',100)
    ]

    shopping_list = []

    if salary.isdigit() :
        salary = int(salary)
        while True:
            for item in product_list:
                print(product_list.index(item),item)
            user_choice = input("选择要买吗,输入q退出>>>:")
            if user_choice.isdigit():
                user_choice = int(user_choice)
                if user_choice < len(product_list) and user_choice >= 0:
                    p_item = product_list[user_choice]
                    if p_item[1] <= salary:
                        shopping_list.append(p_item)
#                        print(shopping_list, "\033[36;1放入购物车成功!\033[0m")
                        salary -=p_item[1]
                        print("\033[36;1m added %s into shopping cart ,your current balance is %s \033[0m" %(p_item,salary))
                    else:
                        print("\033[41;1m 你的余额只剩[%s] \033[0m" %salary)
                else:
                    print("product code [%s] is not exist!" %user_choice)
            elif user_choice == 'q':
                print('------shoping list------')
                for p in shopping_list:
                    with open('./' + username + "/recording.txt", "a+", encoding="utf-8") as f:
                        p = str(p)
                        f.write(p+'\n')
                    print(p)
                with open('./' + username + "/salary.txt", "w+", encoding="utf-8") as f:
                    salary = str(salary)
                    f.write(salary)
                print("your current balance:", salary)
                exit()
            else:
                print("invalid option")

def Query_the_records(choose,username):
    '购物车记录查询'
    if choose == 'y':
        with open('./' + username + "/recording.txt", "r", encoding="utf-8") as f:
            recording = f.read()  # 显示文本内容
            print(recording)
    else:
        pass

流程图

 

github项目地址





猜你喜欢

转载自blog.csdn.net/qq_22510521/article/details/79944794
今日推荐