• python使用笔记009--小练习


    1.密码生成器

     1 '''
     2  1、写一个生产密码的程序,输入几,就产生几条密码,密码产生的不重复。
     3         要求密码:长度6-12,密码必须包含 大写字母、小写字母、数字
     4         产生完密码后存到一个文件里面
     5 '''
     6 import random
     7 import string
     8 #大小写字母列表
     9 #l_chrs = list(string.ascii_letters)
    10 #大写字母
    11 u_chrs = set(string.ascii_uppercase)
    12 #小写字母
    13 lower_chrs = set(string.ascii_lowercase)
    14 #数字列表
    15 nums = [i for i in range(10)]
    16 #将字母列表和数字列表合成一个大的列表
    17 #l_chrs.extend(nums)
    18 l_chrs = set(string.ascii_letters+string.digits)#生成小写字母,大写字母,0-9的数字集合
    19 #批量生成密码
    20 def create_pwd():
    21     pwd_count_str = input('请输入要生成的密码条数:').strip()
    22     if pwd_count_str.isdigit() and int(pwd_count_str) > 0 :#判断输入的密码条数为int类型
    23         #pwd_l = {}#定义一个空的密码列表,用来保存密码
    24         with open('密码.txt','a+',encoding='utf-8') as pwd_file:
    25             #每次执行密码生成器之前都需要清空密码.txt文件
    26             pwd_file.seek(0)
    27             pwd_file.truncate()
    28             for i in range(int(pwd_count_str)):#循环生成密码
    29                 pwd_len = random.randint(6,12)#随机生成密码的长度,范围在6-12之间
    30                 pwd = ran_create_pwd(pwd_len)#随机生成密码
    31                 if pwd in pwd_file.readlines():#如果密码已存在,则重新生成密码
    32                     pwd = ran_create_pwd(pwd_len)
    33                 else:
    34                     pwd_file.write(pwd)
    35     else:
    36         print('输入的密码条数不合格')
    37         create_pwd()
    38 
    39 #根据密码长度随机生成一个密码,密码包含大写字母,小写字母,数字
    40 def ran_create_pwd(pwd_len):
    41     l_chrs_set = set(l_chrs)#将列表转成集合
    42     pwd = random.sample(l_chrs,pwd_len)#从list中随机取N个元素
    43     pwd_set = set(pwd)
    44     #判断密码中包含大写字母,小写字母,数字
    45     if (pwd_set & u_chrs) and (pwd_set & lower_chrs) and (pwd_set & set(nums)):
    46         pwd_str = ''.join(str(i) for i in pwd)+'
    '#将数组转成字符串
    47         return pwd_str
    48     else:
    49         return ran_create_pwd(pwd_len)
    50 
    51 #执行密码生成器
    52 create_pwd()

    2.大乐透号码生成器

     1 '''
     2 写一个生产大乐透号码的程序。
     3         前区号码由01—35共35个号码组成,后区号码由01—12共12个号码组成
     4         01 02 03 04 05 01 08
     5         输入100,产生一百条号码,这一百条不能重复
     6         产生完之后存到文件里面
     7         注意:不能用1 2 3 4 5 1 11  #如果是个位数,要补零
     8 '''
     9 import random
    10 
    11 #数字列表
    12 # nums1 = [i+1 for i in range(35)]
    13 # nums2 = [i+1 for i in range(12)]
    14 nums1 = list(range(1,36))
    15 nums2 = list(range(1,13))
    16 def create_dlts():
    17     dlts_count_str = input('请输入要生成的大乐透号码条数:').strip()
    18     if dlts_count_str.isdigit() and int(dlts_count_str) > 0:  # 判断输入的密码条数为int类型
    19         with open('大乐透号码.txt','a+',encoding='utf-8') as dlthm_file:
    20             #每次执行大乐透号码生成器之前都需要清空大乐透号码.txt文件
    21             dlthm_file.seek(0)
    22             dlthm_file.truncate()
    23             for i in range(int(dlts_count_str)):#循环生成大乐透号码
    24                 dlthm = create_dlthm()
    25                 if dlthm in dlthm_file.readlines():#如果文件中存在该号码,则应该重新生成
    26                     dlthm = create_dlthm()
    27                 else:
    28                     dlthm_file.write(dlthm)#不存在则写入大乐透号码
    29     else:
    30         print('输入的大乐透条数不合格')
    31         create_dlts()
    32 
    33 
    34 def create_dlthm():
    35     temp1 = random.sample(nums1,5)#随机取5个数
    36     temp2 = random.sample(nums2,2)#随机取2个数
    37     #对这两个列表进行升序排序
    38     temp1 = sorted(temp1)
    39     temp2 = sorted(temp2)
    40     temp1.extend(temp2)
    41     dlthm_str = ' '.join(str(i).zfill(2) for i in temp1)+'
    '#将列表中的值个位数补0,十位数不变,转成字符串
    42     return dlthm_str
    43 
    44 #执行大乐透号码生成器
    45 create_dlts()

    3.商品管理

      1 import json
      2 
      3 with open('goods.json', encoding='utf-8') as goods_file:
      4     goods = json.load(goods_file)  # 将商品读取出来,并放入字典中
      5 
      6 def product_manager():
      7     choice_num = input('请输入你的选择:1、查看商品信息2、添加商品3、修改4、删除:').strip()
      8     if choice_num == '1':
      9         show()
     10     elif choice_num == '2':
     11         add_product()
     12     elif choice_num == '3':
     13         modify_product()
     14     elif choice_num == '4':
     15         delete()
     16     else:
     17         print('输入格式有误!!!')
     18         product_manager()#重新输入
     19 
     20 def show():#查询商品
     21     show_type = input('请输入需要查看的商品:all、查看全部商品,具体商品名称、查看单个商品').strip()
     22     if show_type.lower() == 'all':
     23         goods_str = json.dumps(goods,indent=4,ensure_ascii=False)
     24         print('全部商品信息为:%s'%goods_str)
     25     else:
     26         good_temp = goods.get(show_type)#根据传入的单个商品名称,查询单个商品信息
     27         if good_temp:
     28             print('%s的商品信息为:%s'%(show_type,good_temp))
     29         else:
     30             print('你输入的商品信息不存在!!!')
     31 
     32 
     33 def delete():#删除商品
     34     pro_name = input('请输入需要删除的商品名称').strip()
     35     if pro_name:
     36         if goods.get(pro_name):  # 商品已存在,能删除
     37             goods.pop(pro_name)
     38             print('删除商品信息成功')
     39             with open('goods.json', 'w', encoding='utf-8') as goods_del_file:
     40                 json.dump(goods, goods_del_file, indent=4, ensure_ascii=False)  # 将新增商品信息写入文件中
     41         else:
     42             print('商品不存在,删除失败!!!')
     43     else:
     44         print('输入的商品信息不能为空!!!')
     45 
     46 
     47 def add_product():#添加商品
     48     pro_name = input('请输入需要添加的商品名称').strip()
     49     if pro_name:
     50         if goods.get(pro_name):#商品已存在,不能添加
     51             print('商品已存在,不能添加!!!')
     52         else:
     53             good_info = checkProduct()
     54             if good_info:
     55                 goods[pro_name] = good_info
     56                 print('新增商品信息成功')
     57                 with open('goods.json','w', encoding='utf-8') as goods_add_file:
     58                     json.dump(goods, goods_add_file, indent=4, ensure_ascii=False)  # 将新增商品信息写入文件中
     59     else:
     60         print('输入的商品名称不能为空!!!!')
     61 
     62 
     63 def modify_product():#修改商品
     64     pro_name = input('请输入需要修改的商品名称').strip()
     65     if pro_name:
     66         if goods.get(pro_name):  # 商品已存在,可以修改
     67             good_info = checkProduct()
     68             if good_info:
     69                 goods[pro_name] = good_info
     70                 print('修改商品信息成功')
     71                 with open('goods.json', 'w', encoding='utf-8') as goods_modify_file:
     72                     json.dump(goods, goods_modify_file, indent=4, ensure_ascii=False)  # 将修改商品信息写入文件中
     73         else:
     74             print('商品不存在,不能修改!!!')
     75     else:
     76         print('输入的商品名称不能为空!!!!')
     77 
     78 #判断输入的值是否合格,合格则返回三个参数
     79 def checkProduct():
     80     good_info = {}
     81     pro_color = inputProColor()
     82     if pro_color:
     83         pro_price = inputProPrice()
     84         if pro_price:
     85             pro_count = inputProCount()
     86             if pro_count:
     87                 good_info['color'] = pro_color
     88                 good_info['price'] = float(pro_price)
     89                 good_info['count'] = int(pro_count)
     90             else:
     91                 print('输入商品数量不合格')
     92         else:
     93             print('输入商品价格不合格')
     94     else:
     95         print('输入商品颜色不合格')
     96 
     97     return good_info
     98 
     99 
    100 #校验颜色
    101 def checkColor(proColor):
    102     color_list = ['红色','黄色','白色','黑色','绿色','橙色','蓝色','紫色']
    103     color_bool = False
    104     if proColor in color_list:
    105         color_bool = True
    106     return color_bool
    107 
    108 #价格需要是大于0的正整数或者大于0的小数
    109 def checkPrice(proPrice):
    110     boolean = False
    111     s = str(proPrice)
    112     if s.isdigit() and int(s) > 0:#正数可以
    113         boolean = True
    114     elif s.count('.') == 1:
    115         left, right = s.split('.')  # 根据小数点进行分割,价格只能是两位小数
    116         if right.isdigit() and left.isdigit() and (int(left) != 0 or int(right) != 0) and len(right) < 3:
    117             boolean = True
    118     return boolean
    119 
    120 #判断商品数量必须是大于0的整数
    121 def checkProCount(proCount):
    122     boolean = False
    123     s = str(proCount)
    124     if s.isdigit() and int(s) > 0:  # 正数可以
    125         boolean = True
    126 
    127     return boolean
    128 
    129 #输入商品颜色
    130 def inputProColor():
    131     for i in range(3):
    132         pro_color = input('请输入商品颜色:').strip()
    133         if checkColor(pro_color):
    134             return pro_color
    135     else:
    136         print('商品颜色输入次数已用完!!!')
    137         return None
    138 
    139 #输入商品价格
    140 def inputProPrice():
    141     for i in range(3):
    142         pro_price = input('请输入商品价格:').strip()
    143         if checkPrice(pro_price):
    144             return pro_price
    145     else:
    146         print('商品价格输入次数已用完!!!')
    147         return None
    148 
    149 
    150 #输入商品数量
    151 def inputProCount():
    152     for i in range(3):
    153         pro_count = input('请输入商品数量:').strip()
    154         if checkProCount(pro_count):
    155             return pro_count
    156     else:
    157         print('商品数量输入次数已用完!!!')
    158         return None
    159 
    160 product_manager()
    161 #print(checkProCount(1))
  • 相关阅读:
    android-自定义View
    java
    记录-常用的开发技巧
    便签:
    zTree 树形控件 ajax动态加载数据
    jquery ajax 传数据到后台乱码的处理方法
    spring + quartz 定时
    SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法
    复制新项目 ,tomcat部署时名字还是旧项目名
    jsp 通用获取所有表单值传后台
  • 原文地址:https://www.cnblogs.com/cjxxl1213/p/12896945.html
Copyright © 2020-2023  润新知