Python课程第三天作业
一、统计⽂件数据中出现的的所有字符与该字符出现的个数(不区分⼤⼩写,标点与空格也算)
1 2 3 4 5 6 7 8 9 10 | ⽂件内容: hello friend, can you speak English! # 结果: { 'h' : 1, 'e' : 4, 'l' : 3, 'o' : 2, ' ' : 5, ... } |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/usr/bin/env python # -*- coding: utf-8 -*- #统计⽂件数据中出现的的所有字符与该字符出现的个数 #字符串方式 char_dict1 = {} with open( 'test.txt' , 'r' , encoding= 'utf-8' ) as f: f_str = f.read() for k1 in f_str: char_dict1[k1.lower()] = f_str.lower().count(k1.lower()) print(char_dict1) #seek方式 char_dict2 = {} with open( 'test.txt' , 'r' , encoding= 'utf-8' ) as f2: for k2 in f2.read(): f2.seek(0,0) char_dict2 [k2.lower()] = f2.read().lower().count(k2.lower()) print(char_dict2 ) #列表方式 char_dict3={} char_lsit=list() with open( 'test.txt' , 'rt' ,encoding= 'utf-8' ) as f: for k5 in f.read(): char_lsit.append(k5.lower()) f.seek(0, 0) for k6 in f.read(): if k6 in char_lsit: char_dict3[k6]=char_lsit.count(k6) print(char_dict3) |
二、 统计⽂件中⼤写字⺟、⼩写字⺟、数字及其他字符出现的次数
1 2 3 4 5 6 7 8 | # ⽂件内容: Abc123,-+XYZopq000.?/ # 结果: { '⼤写字⺟' : 4, '⼩写字⺟' : 5, '数字' : 6, '其他字符' : 6 } |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #!/usr/bin/env python # -*- coding: utf-8 -*- #统计⽂件中⼤写字⺟、⼩写字⺟、数字及其他字符出现的次数 capital_count=0 lowercase_count=0 number_count=0 special_characters_count=0 with open( 'test2.txt' , 'rt' ,encoding= 'utf-8' ) as f: string =f.read() for i in string : if (ord(i) >= 33 and (ord(i)<= 47)) or ((ord(i) >= 58 and (ord(i)<= 64) )): special_characters_count += 1 elif ord(i) >= 48 and (ord(i)<= 57): number_count += 1 elif ord(i) >= 65 and (ord(i)<= 90): capital_count += 1 elif ord(i) >= 97 and (ord(i)<= 122): lowercase_count += 1 string_count = { '大写字母' :capital_count, '小写字母' :lowercase_count, '数字' :number_count, '特殊字符' :special_characters_count} print(string_count) |
三、登录注册系统
1 2 3 4 5 6 7 | # 需求分析: 1.可以循环登录注册,输⼊1代表选择登录功能,输⼊2代表注册功能,输⼊0代表退出,其他输⼊代表 输⼊有误,重输 2.⽤户的账号密码信息存放在usr.txt⽂件中,保证⽤户注册成功后,重启系统,⽤户信息仍然保存 3.登录在账号验证通过才输⼊密码验证登录,账号验证三次失败⾃动进⼊注册功能,登录三次验证失败 ⾃动退出系统 4.第⼀次注册,⽂件写⼊ 账号:密码 信息,再次注册追加写⼊ |账号:密码 信息 |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #!/usr/bin/env python # -*- coding: utf-8 -*- import time white_user_set=[] black_user_set=[] user_list=[] white_userDB= 'userDB' black_userDB= 'user_blackDB' with open(black_userDB, 'r+' ,encoding= 'utf-8' ) as black_f: for black_user in black_f.readlines(): black_user = black_user.strip( "\n" ) black_user_set.append(black_user) with open(white_userDB, 'r+' ) as white_f: for file in white_f.readlines(): file = file.strip( "\n" ) user_field,passwd_field,remarks_field= file.split( '|' ) name1_field, user=user_field.split( ':' ) name2_field, passwd = passwd_field.split( ':' ) name3_field, remarks = remarks_field.split( ':' ) white_user_set.append({name1_field:user,name2_field:passwd,name3_field:remarks}) user_list.append(user) print(white_user_set) def mod_info(file_name,old_str,new_str): mod_date= '' with open(file_name, 'r+' ,encoding= 'utf-8' ) as f: for line in f: line = line.replace(old_str,new_str) mod_date += line with open(file_name, 'w' , encoding= 'utf-8' ) as f1: f1.writelines(mod_date) #用户注册函数 def user_register(): count=0 while True: count+=1 register_name = input( '创建用户名:' ) register_passwd = input( '创建密码:' ) register_passwd1 = input( '创建密码:' ) user_remarks = input( '输入您的备注信息,默认为空:' ) if user_remarks == '' : user_remarks = 'null' if register_passwd == register_passwd1: if register_name in user_list: print( '用户已存在,请重新创建' ) else : user_information=register_name+ ':' +register_passwd+ ':' +user_remarks print(user_information) print(type(user_information)) with open(white_userDB, 'a+' ) as f: f.write( '\n' ) f.write(user_information) print( '恭喜您,用户创建成功!!!' ) return 0 else : if count == 3: print( '失败次数超过上限,程序退出' ) exit(3) print( '两次密码不一致,请重新创建2' ) <br>#用户登录函数 def user_login(): Flag = True user_failures = 0 all_failures = 0 while Flag: login_name = input( '请输入用户名:' ) login_passwd = input( '输入密码:' ) print(login_name,login_passwd) if login_name in black_user_set: print( '该用户已被锁定,请联系您的管理员!' ) exit(3) if login_name in user_list: for subscript,line in enumerate(white_user_set): if (login_name == line[ 'name' ]) and (login_passwd == line[ 'passwd' ]): print( '登录成功' ) print( '正在登录。请稍后' ) time.sleep(1) print( '' ) print( '=' *30) print( '欢迎来到 250 世界' ) print( '=' *30) Flag = False break for subscript,line in enumerate(white_user_set): if (login_name == line[ 'name' ]) and (login_passwd != line[ 'passwd' ]): user_failures += 1 all_failures += 1 if (user_failures == 3): print( '验证失败次数过多,用户被锁定' ) with open(black_userDB, 'a+' ) as f: f.write(login_name+ '\n' ) exit(2) print( '用户名或密码不正确,登录失败,请重新登录 2 ' ) break else : print( '用户名或密码不正确,登录失败,请重新登录 3 ' ) all_failures += 1 if all_failures == 6: print( '失败次数过多,请确认后再登录 4' ) exit(1) #用户信息修改函数 def user_info_mod(user_name,user_passwd,remarks): user_login print( '' ' 1.修改密码 2.修改备注信息 '' ') info=input( '请根据提示,选择您要修改的用户信息' ) if info == '1' : confirm_password = input( '请输入您当前的密码:' ) new_password = input( '请输入您要修改的密码:' ) if confirm_password == user_passwd: print( '您输入的当前密码有误,请程序输入!' ) else : for subscript, line in enumerate(white_user_set): if (user_name == line[ 'name' ]): line[passwd]=new_password mod_info(white_userDB, confirm_password, new_password ) elif info == 2: new_remarks=input( '请输入您要修改的备注:' ) for subscript, line in enumerate(white_user_set): if (user_name == line[ 'name' ]): line[remarks]=new_remarks mod_info(white_userDB, line[remarks], new_remarks) else : print( '无效的输入,请重新输入!' ) #主函数 def main(): while True: count=0 print( '1 注册用户:' ) print( '2 登录系统:' ) print( '3 修改备注信息:' ) user_operation=input( '请选择您的操作:' ) print(type(user_operation)) if user_operation == '1' or user_operation == '3' : user_register() break elif user_operation == '2' : user_login() break else : count+=1 if count == 3: print( '失败次数过多,请确认后再登录' ) exit(3) print( '非法的输入,请重试' ) if __name__ == '__main__' : main() |
"一劳永逸" 的话,有是有的,而 "一劳永逸" 的事却极少
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具