# 帐户表account: # sylar:123 # alex:456 # wusir:789 # taibai:789 # 需熟练的知识点:文件操作with open()/write()/read()、去掉所有空格strip()、切割split()、所有字母大写upper() # 循环for...in...、判断if...else...
def regist(): # 输入数据 # 用户名若存在则不通过:注册失败 # 通过则存入account: mode="r+" print("请输入用户名及密码完成注册") username = input("请输入注册用户名:") password = input("请输入密码") with open("account", mode="r+", encoding="utf-8") as f: for line in f: # sylar:123 if username == line.strip().split(":")[0]: print("对不起,该用户已注册") break else: f.write("\n"+username+":"+password) print("注册名为:%s" % username) print("注册的密码为:%s" % password) regist() def login(): count = 3 while count > 0: count -= 1 username = input("请输入用户名:").strip() password = input("请输入密码:").strip() with open("account", mode="r", encoding="utf-8") as f: for item in f: if username +":"+password == item.strip(): return True else: print("密码错误,您还有%s 次机会" % count) return "程序退出" while 1: print("注册: 1 登录: 2 退出: Q") num = input("请输入:").strip() if num.upper() == "Q": break elif num == "1": regist() elif num == "2": if login(): print("登录成功!") break else: print("登录失败!") else: print("输入错误! 重新输入!")