zoukankan      html  css  js  c++  java
  • Day3笔记(作业)

    1、写一个产生随机密码的函数,
      1、输入一个数字,就产生多少条密码
      2、密码长度在8-15直接
      3、密码必须包括大写字母、小写字母、数字和特殊字符
      4、密码不能以特殊字符串开头
      5、密码保存到文件里面

    import random
    import string
    
    def op_file(file_name,content=None):
        if content:
            with open(file_name,'w',encoding='utf-8') as fw:
                fw.write(content)
        else:
            with open(file_name,encoding='utf-8') as fr:
                return fr.read()
    
    def gen_password(num):#产生随机密码函数
        all_password = set()
        while len(all_password)!= num:
            length = random.randint(8,15)#密码长度在8-15字节
            temp = random.sample(string.digits+string.ascii_uppercase+string.ascii_lowercase+string.punctuation,length)
            if set(temp) & set(string.digits) and set(temp) & set(string.ascii_uppercase) and set(temp) & set(string.ascii_lowercase) 
                    and set(temp) & set(string.punctuation) and not set(temp[0]) & set(string.punctuation):
                #密码必须包括大写字母、小写字母、数字和特殊字符且不以特殊字符开头
                password = ''.join(temp)
                all_password.add(password+'
    ')
        return all_password
    
    passwords = gen_password(100)
    
    password_str = ''.join(passwords)
    op_file('passwords.txt',password_str)
    
    # string.ascii_letters#大小写字母
    # string.ascii_lowercase#小写字母
    # string.ascii_uppercase#大写字母
    # string.punctuation#所有特殊符号

    2、写一个管理商品的程序。
    1、添加商品的时候,要输入
    商品名称
    价格
    数量
    商品已经存在要提示
    价格可以是小数和整数,但是要大于0
    数量也要大于0
    2、修改商品
    商品名称
    价格
    数量
    商品存在才可以修改
    3、删除商品
    输入商品名称
    商品不存在要提示
    4、查看商品
    直接把所有print出来就行了

    import json
    import os  #引入os模块
    
    def is_positive_float(str1):
        if str1.count('.') == 1:
            left, right = str1.split('.')
            if left.isdigit() and right.isdigit():
                return True
            else:
                return False
        elif str1.isdigit() and int(str1) > 0:
            return True
        return False
    
    def add_product():
        with open('product.json', 'a+', encoding='utf-8') as f:   #以a+模式打开文件,如果存在判断是否有商品
            f.seek(0)    #文件指针放到最前面以便读取文件
            if os.path.getsize('product.json'): #有商品读出商品,放到字典里
                product = json.load(f)
            else:
                product = {}       #没有商品不告诉用户,直接往下走
        name = input('请输入要添加的商品名称:').strip()
        price = input('请输入商品价格:').strip()
        count = input('请输入商品数量:').strip()
        if name in product:
            print('您添加的商品已经存在')
        elif not is_positive_float(price):
            print('您添加的商品价格不是大于0的数字')
        elif not count.isdigit() or int(count) == 0:
            print('您添加的商品数量不是大于0的整数')
        else:
            product.setdefault(name, {'price': eval(price), 'count': eval(count)})  #加一个eval,json中的值就没有“”
        with open('product.json', 'w', encoding='utf-8') as f:  #以清空原文件的写模式写入完整的字典数据
            json.dump(product, f, ensure_ascii=False, indent=4)
    
    def modify_product():
        with open('product.json', 'r+', encoding='utf-8') as f:
            f.seek(0)
            if os.path.getsize('product.json'):
                product = json.load(f)
            else:
                print("暂无商品,您需要先去添加商品")
                return       #文件不存在结束函数
        name = input('请输入要修改的商品名称:').strip()
        price = input('请输入商品价格:').strip()
        count = input('请输入商品数量:').strip()
        if name not in product:
            print('您输入的商品不存在!')
        elif not is_positive_float(price):
            print('您添加的商品价格不是大于0的数字')
        elif not count.isdigit() or int(count) == 0:
            print('您添加的商品数量不是大于0的整数')
        else:
            product[name] = {
                'price': eval(price),#加一个eval,json中的值就没有“”
                'count': eval(count),#加一个eval,json中的值就没有“”
            }
        with open('product.json', 'w', encoding='utf-8') as fw:  # 以清空原文件的写模式写入完整的字典数据
            json.dump(product, fw, ensure_ascii=False, indent=4)
    
    def del_product():
        with open('product.json', 'a+', encoding='utf-8') as f:
            f.seek(0)
            if os.path.getsize('product.json'):
                product = json.load(f)
            else:
                print("暂无商品,您需要先去添加商品")
                return       #文件不存在结束函数
        name = input('请输入要删除的商品名称:').strip()
        if name not in product:
            print('您输入的商品不存在!')
        else:
            product.pop(name)
        with open('product.json', 'w', encoding='utf-8') as f:
            if product:
                json.dump(product, f, ensure_ascii=False, indent=4)
            else:
                f.truncate()     #删除了所以商品后,product是空字典,继续写入会写入{},需要直接清空字典
    
    def show_product():
        with open('product.json', 'a+', encoding='utf-8') as f:
            f.seek(0)
            if os.path.getsize('product.json'):
                product = json.load(f)
                return product
            else:
                print('暂无商品,请先添加商品')
    
    choice = input('请选择您的操作:1.添加商品 2.修改商品 3.删除商品 4.查看商品
    ')
    if choice == '1':
        add_product()
    elif choice == '2':
        modify_product()
    elif choice == '3':
        del_product()
    elif choice == '4':
        print(show_product())
    else:
        print('输入有误')
    
    # 1,文件内容为空时使用json.load(f)报错,json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0),所以要加一个判断文件内容是否为空。
    # 2,当文件内已有商品时,a+模式下一开始没有seek(),导致有商品也未读到内容。
    # 3,判断输入价格是否是大于0的数字时专门定义了一个函数来判断。
    # 4,出现一些和以前的认知不符的结果时应该找找原因,肯定是哪里出问题了才会导致这样的结果,而不是直接否定之前的认识。
  • 相关阅读:
    【Luogu】P3809后缀排序(后缀数组模板)
    【Luogu】P2709小B的询问(莫队算法)
    【Luogu】P2766最长不下降子序列问题(暴力网络流)
    【Luogu】P2486染色(树链剖分)
    【bzoj】P4407于神之怒加强版(莫比乌斯反演)
    【Luogu】P3343地震后的幻想乡(对积分概率进行DP)
    【Luogu】P2146软件包管理器(树链剖分)
    【Luogu】P3159交换棋子(超出我能力范围的费用流)
    【Luogu】P2569股票交易(单调队列优化DP)
    【Luogu】P2219修筑绿化带(单调队列)
  • 原文地址:https://www.cnblogs.com/lz523/p/11025853.html
Copyright © 2011-2022 走看看