python作业习题集锦

1. 登录作业:

  写一个登录程序,登录成功之后,提示XXX欢迎登录,登录失败次数是3次,要校验一下输入为空的情况

复制代码
 1 for i in range(3):
 2     username=input('username:').strip()
 3     passwd=input('passwd:').strip()
 4     if username and passwd:
 5         if username=='weixiaocui' and passwd=='123456':
 6             print('%s欢迎登录'%username)
 7             break
 8         else:
 9             print('账号/密码错误')
10     else:
11         print('账号/密码不能为空')
12 else:
13     print('错误失败次数太多!')
复制代码

 2.登录,注册,账号密码 存在文件里面,登录用注册时候的账号和密码

复制代码
 1 f=open('user.txt','a+',encoding='utf-8')
 2 f.seek(0)
 3 all_users={}
 4 for line in f:
 5     if line:
 6         line=line.strip()
 7         line_list=line.split(',')
 8         all_users[line_list[0]]=line_list[1]
 9 while True:
10     username=input('用户名:').strip()
11     passwd=input('密码:').strip()
12     cpasswd=input('确认密码:').strip()
13     if username and passwd and cpasswd:
14         if username in all_user:
15             print('用户名已经存在,请重新输入!')
16         else:
17             if passwd==cpasswd:
18                 all_users[username]=passwd
19                 f.write(username+','+passwd+'\n')
20                 f.flush()#立即写到文件里面
21                 print('注册成功!')
22                 break
23             else:
24                 print('两次输入密码不一致,请重新输入!')
25     else:
26         print('用户名/密码/确认密码不能为空!')
27 f.close()
复制代码
复制代码
 1 #登录作业代码
 2 f=open('user.txt',encoding='utf-8')
 3 all_users={}#存放所有用户
 4 for line in f:
 5     if line:
 6         line=line.strip()
 7         line_list=line.split(',')
 8         all_users[line_list[0]]=line_list[1]
 9 while True:
10     username=input('用户名:').strip()
11     passwd=input('密码:').strip()
12     if username and passwd:
13         if username in all_user:
14             if passwd == all_users.get(username):
15                 print('欢迎光临 %s'%username)
16                 break
17             else:
18                 print('密码输入错误!')
19         else:
20             print('用户不存在!')
21     else:
22         print('用户名/密码不能为空!')
23 f.close()
复制代码

3.生成8位密码,输入一个生成的条数,然后生成包含大写,小写字母,数字的密码,写到文件里面

复制代码
 1 import random,string
 2 count=int(input('请输入你要生成密码的条数:'))
 3 all_passwds=[]
 4 while True:
 5     lower=random.sample(string.ascii_lowercase,1)#随机取一个小写字母
 6     upper=random.sample(string.ascii_uppercase,1)#随机取一个大写字母
 7     num=random.sample(string.digits,1)#随机取一个数字
 8     other=random.sample(string.ascii_letters+string.digits,5)
 9     res=lower+upper+num+other#把这4个list合并到一起
10     random.shuffle(res)#打乱顺序
11     new_res=''.join(res)+'\n'#因为res是list,所以转成字符串
12     if new_res not in all_passwds:#这里是为了判断是否重复
13         all_passwds.append(new_res)
14     if len(all_passwds)==count:#判断循环什么时候结束
15         break
16 with open('passwds.txt','w') as fw:
17     fw.writelines(all_passwds)
复制代码

 4.判断字典里面不一样的key,字典可能有多层嵌套

  循环字典,从一个字典里面取到key,然后再从第二个字典里面取k,判断value是否一样

复制代码
 1 ok_req={
 2     "version": "9.0.0",
 3     "is_test": True,
 4     "store": "",
 5     "urs": "",
 6     "device": {
 7         "os": "android",
 8         "imei": "99001062198893",
 9         "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
10         "mac": "02:00:00:00:00:00",
11         "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
12         "udid": "a34b1f67dd5797df93fdd8b072f1fb8110fd0db6",
13         "network_status": "wifi"
14     },
15     "adunit": {
16         "category": "VIDEO",
17         "location": "1",
18         "app": "7A16FBB6",
19         "blacklist": ""
20     },
21     "ext_param":{
22         "is_start" : 0,
23         "vId":"VW0BRMTEV"
24     }
25 }
26 not_ok={
27     "version": "9.0.0",
28     "is_test": True,
29     "urs": "",
30     "store": "",
31     "device": {
32         "os": "android",
33         "imei": "99001062298893",
34         "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
35         "mac": "02:00:00:00:00:00",
36         "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
37         "udid": "a34b1f67dd5797da93fdd8b072f1fb8110fd0db6",
38         "network_status": "wifi"
39     },
40     "adunit": {
41         "category": "VIDEO",
42         "location": "1",
43         "app": "7A16FBB6",
44         "blacklist": ""
45     },"ext_param": {
46         "is_start": 0,
47         "vid": "VW0BRMTEV"
48     }
49 }
50 def compare(d1,d2):
51     for k in d1:
52         v1=d1.get(k)
53         v2=d1.get(k)
54         if type(v1)==dict:
55             compare(v1,v2)
56         else:
57             if v1!=v2:
58                 print('不一样的k是【%s】,v1是【%s】 v2是【%s】'%(k,v1,v2))
59     #对称差集,两个集合里面都没有的元素,在a里有,在b里没有,在b里有,在a里没有的
60     res=set(d1.keys()).symmetric_difference(set(d2.keys()))
61     if res:
62         print('不一样的k',res)
63 compare(ok_req,not_ok)
复制代码

5.商品管理的程序

  1.添加商品

  2.查看商品

  3.删除商品

  4.商品都存在文件里面

  5.存商品的样式:{‘car’:{‘color’:’red’,’count’:5,’price’:100},’car2’:{‘color’:’red’,’count’:5,’price’:100}}

复制代码
 1 PRODUCTS_FILE='products'
 2 def op_file(filename,content=None):
 3     f=open(filename,'a+',encoding='utf-8')
 4     f.seek(0)
 5     if content!=None:
 6         f.truncate()
 7         f.write(str(content))
 8         f.flush()
 9     else:
10         res=f.read()
11         if res:
12             products=eval(res)
13         else:
14             products={}
15         return products
16     f.close()
17 def check_price(s):
18     s = str(s)
19     if s.count('.')==1:
20         left=s.split('.')[0]
21         right=s.split('.')[1]
22         if left.isdigit() and right.isdigit():
23             return True
24     elif s.isdigit() and int(s)>0:
25         return True
26     return False
27 def check_count(count):
28     if count.isdigit():
29         if int(count)>0:
30             return True
31         return False
32 def add_product():
33     while True:
34         name=input('name:').strip()
35         price=input('price:').strip()
36         count=input('count:').strip()
37         color=input('color:').strip()
38         if name and price and count and color:
39             products=op_file(PRODUCTS_FILE)
40             if name in products:
41                 print('商品已经存在')
42             elif not check_price(price):
43                 print('价格不合法')
44             elif not check_count(count):
45                 print('商品数量不合法')
46             else:
47                 product={'color':color,'price':'%.2f'%float(price),'count':count}
48                 products[name]=product
49                 op_file(PRODUCTS_FILE,products)
50                 print('商品添加成功')
51                 break
52         else:
53             print('必填参数未填')
54 def view_products():
55     products=op_file(PRODUCTS_FILE)
56     if products:
57         for k in products:
58             print('商品名称【%s】,商品信息【%s】'%(k,products.get(k)))
59     else:
60         print('当前没有商品')
61 def del_products():
62     while True:
63         name=input('name:').strip()
64         if name:
65             products=op_file(PRODUCTS_FILE)
66             if products:
67                 if name in products:
68                     products.pop(name)
69                     op_file(PRODUCTS_FILE,products)
70                     print('删除成功!')
71                     break
72                 else:
73                     print('你输入的商品名称不存在,请重新输入!')
74             else:
75                 print('当前没有商品')
76         else:
77             print('必填参数未填')
78 def start():
79     choice=input('请输入你的选择:1、添加商品2、查看商品3、删除商品,q、退出:').strip()
80     if choice=='1':
81         add_product()
82     elif choice=='2':
83         view_products()
84     elif choice=='3':
85         del_products()
86     elif choice=='q':
87         quit('谢谢光临')
88     else:
89         print('输入错误!')
90         return start()
91 start()
复制代码

 6.写清理日志的一个脚本,要求转入一个路径,只保留3天以内的日志,剩下的全部删掉。

复制代码
 1 import os,datetime
 2 def clean_log(path):
 3     if os.path.exists(path) and os.path.isdir(path):
 4         today = datetime.date.today()  #2017-01-02
 5         yesterday = datetime.date.today()+ datetime.timedelta(-1)
 6         before_yesterday = datetime.date.today()+ datetime.timedelta(-2)
 7         file_name_list = [today,yesterday,before_yesterday]
 8         for file in os.listdir(path):
 9             file_name_sp = file.split('.')
10             if len(file_name_sp)>2:
11                 file_date = file_name_sp[1] #取文件名里面的日期
12                 if file_date not in file_name_list:
13                     abs_path = os.path.join(path,file)
14                     print('删除的文件是%s,'%abs_path)
15                     os.remove(abs_path)
16                 else:
17                     print('没有删除的文件是%s'%file)
18     else:
19         print('路径不存在/不是目录')
20 clean_log(r'')
复制代码
0
0


currentDiggType = 0;

« 上一篇: python学习笔记(二)列表操作
» 下一篇: python学习笔记(三)字典操作和元组操作
    </div>

猜你喜欢

转载自blog.csdn.net/ccbulougen/article/details/81189402