写一个登陆注册功能
时间: 2023-07-01 19:21:22 浏览: 59
好的,以下是一个简单的登陆注册功能的示例代码:
```
import hashlib
users = {}
def register():
username = input("Please enter your username: ")
password = input("Please enter your password: ")
hashed_password = hashlib.sha256(password.encode()).hexdigest()
users[username] = hashed_password
print("Registration successful!")
def login():
username = input("Please enter your username: ")
password = input("Please enter your password: ")
hashed_password = hashlib.sha256(password.encode()).hexdigest()
if users.get(username) == hashed_password:
print("Login successful!")
else:
print("Incorrect username or password.")
while True:
print("Please choose an option:")
print("1. Register")
print("2. Login")
choice = input()
if choice == "1":
register()
elif choice == "2":
login()
else:
print("Invalid choice. Please try again.")
```
这个示例代码使用了一个字典 `users` 来存储已注册的用户的用户名和密码(经过哈希处理)。当用户选择注册时,程序会要求输入用户名和密码,并将其加入到 `users` 中;当用户选择登陆时,程序会要求输入用户名和密码,然后将密码哈希处理后与 `users` 中对应的哈希值进行比较,如果一致则登陆成功,否则登陆失败。