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_users:
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=d2.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'')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM