【week1错题集】
day9【2.f】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# day9 题2.f ''' 有如下文件,t1.txt,里面的内容为: 葫芦娃,葫芦娃, 一根藤上七个瓜 风吹雨打,都不怕, 啦啦啦啦。 以r模式打开文件,从‘风吹雨打.....’开始读取,一直读到最后。 ''' with open("t1.txt",mode="r",encoding="utf-8") as fp: lst = fp.readlines() for i in lst[2:]: print(i)
打印菱形小星星
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# day6 打印菱形小星星 """ # 行数和空格之间的关系 -6 -> 5 -5 -> 4 -4 -> 3 -3 -> 2 -2 -> 1 -1 -> 0 0 -> 跳过 # 行数 和 星星之间的关系 n = 11 -6 -> 1 -5 -> 3 -4 -> 5 13 - abs(i) * 2 13 - 12 = 1 13 - 10 = 3 13 - 8 = 5 """ # abs用来算绝对值 n = 29 hang = n // 2 + 1 for i in range(-hang, hang + 1): if i == 0: continue # 打印空格 print(" " * (abs(i) - 1), end="") # 打印星星 print("*" * (n + 2 - abs(i) * 2))
任意个数字符串的拼接
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 任意个数字符串的拼接 """ 班长:宋云杰 班花:刘彩霞 班草:刘子涛 吃瓜群众:牧树人,晏国彰,刘聪 """ def func(**kwargs): print(kwargs) # 定义一个字典,存放每一位同学的身份 dic = {"monitor":"班长","class_flower":"班花","class_grass":"班草"} strvar1 = "" strvar2 = "" # 遍历字典中的键值对 for k,v in kwargs.items(): print(k,v) # monitor 宋云杰 class_flower 刘彩霞 class_grass 刘子涛 .... # 判断键是否在字典当中,如果在,获取对应的身份,拼装成字符串 if k in dic: strvar1 += dic[k] + ":" + v + "\n" # 班长 + ":" + 宋云杰 + '\n' else: strvar2 += v + "," # 去掉右边多余的\n和,形成最后的效果 print(strvar1.strip()) print("吃瓜群众:"+ strvar2.rstrip(",")) func(monitor="宋云杰",class_flower="刘彩霞",class_grass="刘子涛",eatgua1="牧树人",eatgua2="晏国彰",eatgua3="刘聪")
登录注册程序
# 注册: # 1.检测两次密码如果相同,确认注册成功 # 2.检测两次密码如果不同,提示两次密码不一致 # 3.用户名不能重复 # accountlst存放所有账号 accountlst = [] sign = True with open("user.txt",mode="a+",encoding="utf-8") as fp: while sign: # 输入用户名 username = input("请输入要注册的用户名:") if username == "" or " " in username: print("对不起,该名字含有非法字符") else: # 把数据从文件当中获取 fp.seek(0) lst = fp.readlines() for i in lst: account = i.strip().split(":")[0] accountlst.append(account) print(accountlst) # 判断该名字是否已经存在 if username in accountlst: print("抱歉,改名字已经使用~") else: # 检测密码 pwd = input("请输入您的密码") while True: pwd2 = input("请确认您的密码,按q退出~") # 如果密码相同 if pwd == pwd2: # 拼接字符串 strvar = username + ":" + pwd + "\n" # 写入文件 fp.write(strvar) # 终止循环 sign = False # 打印信息 print("注册成功~") break elif pwd2.upper() == "Q": # sign = False break else: print("对不起,两次密码不一致~")
# ### 登录 # 1.账户密码会预先记录在文件中 # 2.输入账号密码:如果都正确,登录成功 # 3.如果用户登录时进行三次校验,都不对,记录黑名单文件中 # 4.如果是黑名单的用户,禁止下次再次登录 # 存账户 accountlst = [] # 存密码 pwdlst = [] # 存黑名单 blacklst = [] sign = True while sign: username = input("请输入您的用户名:") # 获取文件当中的账号和密码 with open("user.txt",mode="a+",encoding="utf-8") as fp: fp.seek(0) lst = fp.readlines() for i in lst: user,pwd = i.strip().split(":") # print(username,password) accountlst.append(user) pwdlst.append(pwd) # print(accountlst) # print(pwdlst) # 判断该用户是否存在 if username in accountlst: # 判断当前用户是否是黑名单中的用户 with open("black.txt",mode="a+",encoding="utf-8") as fp: fp.seek(0) lst = fp.readlines() for i in lst: blacklst.append(i.strip()) if username in blacklst: print("抱歉,该用户已经被冻结了") else: # 如何找到该账户对应的密码呢? num = accountlst.index(username) pwd = pwdlst[num] # print(num,pwd) """""" # 判断用户输入的密码是否正确 times = 1 while times <= 3: pwd2 = input("请输入您的密码") # 输入成功情况下,终止循环,程序结束 if pwd == pwd2: print("登录成功") sign = False break else: # 剩余的次数 = 总次数 - 使用次数 print("你还剩下%s次机会" % (3 - times)) # 如果用掉了所有的次数,直接加入黑名单,防止下次登录 if times == 3: with open("black.txt",mode="a+",encoding="utf-8") as fp: strvar = username + "\n" fp.write(strvar) print("抱歉,您的账户已经被冻结~") times += 1 else: print("该用户不存在~")