设计一个登录注册功能的程序,首先通过键盘接收登录的用户名和密码,将其写入log.txt文件,表示注册成功。再进行登录测试,需要使用正确的用户名和密码方可成功登录,否则,尝试三次之后,登录失败,提示“明
解析:首先需要实现注册功能,接收用户名和密码,将其写入log.txt文件。然后实现登录功能,读取log.txt文件中的用户名和密码进行验证,尝试三次后提示登录失败。
代码如下:
```python
def register():
username = input("请输入用户名:")
password = input("请输入密码:")
with open("log.txt", "a") as f:
f.write(f"{username} {password}
")
print("注册成功")
def login():
for i in range(3):
username = input("请输入用户名:")
password = input("请输入密码:")
with open("log.txt", "r") as f:
lines = f.readlines()
for line in lines:
user, pwd = line.strip().split()
if user == username and pwd == password:
print("登录成功")
return
print("用户名或密码错误,请重新输入")
print("明天继续吧")
if __name__ == "__main__":
while True:
operation = input("请选择操作(1. 注册 2. 登录 3. 退出):")
if operation == "1":
register()
elif operation == "2":
login()
elif operation == "3":
break
else:
print("输入错误,请重新输入")
```
运行上述代码,根据提示选择注册或登录操作。
代码如下:
```python
def register():
username = input("请输入用户名:")
password = input("请输入密码:")
with open("log.txt", "a") as f:
f.write(f"{username} {password}
")
print("注册成功")
def login():
for i in range(3):
username = input("请输入用户名:")
password = input("请输入密码:")
with open("log.txt", "r") as f:
lines = f.readlines()
for line in lines:
user, pwd = line.strip().split()
if user == username and pwd == password:
print("登录成功")
return
print("用户名或密码错误,请重新输入")
print("明天继续吧")
if __name__ == "__main__":
while True:
operation = input("请选择操作(1. 注册 2. 登录 3. 退出):")
if operation == "1":
register()
elif operation == "2":
login()
elif operation == "3":
break
else:
print("输入错误,请重新输入")
```
运行上述代码,根据提示选择注册或登录操作。