zoukankan      html  css  js  c++  java
  • 第二周作业购物车程序

    #coding:utf-8
    #Author:Mr Zhi
    file_open = open('购物车用户信息','r+',encoding="utf-8")  #购物车用户信息文件保存着用户名密码和余额
    f = str(file_open.read())
    for line in f:
         file_str = str(f)
    file_open_dict = eval(file_str) #转换成字典(文件内容原先是字典)
    username = input("输入用户名:")
    password = input("输入密码:")
    while True:
        if username in file_open_dict:  #判断用户名是否在购物车用户信息文件中
            if password in file_open_dict[username]:
                salary = int(file_open_dict[username][password])
                print('''33[32;1m欢迎登录,当前余额为%s33[0m''' % salary)
                break
            else:
                password = input("密码错误,请重新输入密码")
                continue
        else:
            password_salary = {} #定义密码,工资空字典
            salary_str = input("欢迎第一次登陆,请输入工资:")
            salary =float(salary_str) #输入的工资转成数字
            password_salary[password] = salary  #密码与数字对应
            file_open_dict[username] = password_salary  #用户名和密码工资对应
            file_open.seek(0)  #文件读取到开头
            file_open.write(str(file_open_dict)) #把用户名密码和工资写到文件中
            file_open.tell()   #返回当前位置
            break
    product_list = [   #购物清单
        ['iphone6', 5000],
        ['bike', 800],
        ['python books', 200],
        ['bag', 300],
        ['macbook pro', 9000],
    ]
    history_f = open('history','r+',encoding="utf-8")  #打开history文件
    f2 = str(history_f.read())
    for line in f2:
         file_str2 = str(f2)
    history_line = eval(file_str2)
    if username not in history_line:
        history_line[username] = []
    shoppinglist = history_line[username]
    shoppinglist_new = []
    choice = input ("33[35;1m是否需要查询历史购物记录(y/n)33[0m") #询问是否查询历史记录
    if choice == "y" or choice =="Y":
        print("---历史购物记录---")
        print(shoppinglist)
    while True:
        print("---商品清单---")
        for index,item in enumerate(product_list): #对元组进行遍历并加上索引
            print(index,item) #输出商品
        choice = input("输入商品编码:")
        #choice = int(choice)
        if choice.isdigit():
            choice = int(choice)
            if choice < len(product_list) and choice >= 0:
                p_item = product_list[int(choice)] #把商品赋值给p_item
                print(p_item)
                if p_item[1] <= salary: #判断商品价格和工资的大小
                    shoppinglist_new.append(p_item) #把商品加到元组
                    salary -= p_item[1] #余额 = 工资 - 商品
                    print("33[37;1m买了 %s 还剩下 %s 元33[0m" % (p_item, salary))
                else:
                    print("你的余额不足")
            else:
                print("你选择的编码%无效" % choice)
        elif choice == "q" or choice =="Q":
            file_open_dict[username][password] = salary #工资给对应的用户密码位置
            file_open.seek(0)
            file_open.write(str(file_open_dict)) #把余额写到购物车用户信息文件中
            file_open.tell()
            print("----已购商品清单----")
            print(shoppinglist_new)  # 打印清单
            print("33[31;1m你的余额:%s33[0m" % salary)  # 打印余额
            shoppinglist.extend(shoppinglist_new)  # 本次购物记录追加到购物列表中
            history_line[username] = shoppinglist  # 购物列表和用户名对应
            history_f.seek(0) #移动文件读取指针到开头
            history_f.write(str(history_line))  # 购物记录写入文件
            history_f.tell() #返回文件的当前位置
            break
        else:
            print("你选择的编码%无效" % choice)
  • 相关阅读:
    [置顶] .net技术类面试、笔试题汇总1
    [置顶] Linux协议栈代码阅读笔记(一)
    JS 获取浏览器窗口大小
    huapin
    display:table-cell几种应用
    flex布局大讲解
    chrome的source工具
    linux配置yum源
    oracle Can't connect to X11 window server using ':0.0' /Checking monitor: must be configured to display at least 256 colors解决方法
    oracle Recyclebin
  • 原文地址:https://www.cnblogs.com/wenwei-blog/p/6872686.html
Copyright © 2011-2022 走看看