python用户登录程序_「Python」每日一练:函数的应用之注册登录程序
时间: 2024-01-24 12:20:45 浏览: 18
好的,这里是一个简单的Python用户登录程序示例,包括注册和登录功能。它使用了函数的应用。
```python
user_data = {}
def register():
print("Please enter your details below:")
username = input("Username: ")
password = input("Password: ")
user_data[username] = password
print("Registration successful.")
login()
def login():
print("Please enter your login details below:")
username = input("Username: ")
password = input("Password: ")
if username in user_data and user_data[username] == password:
print("Login successful!")
else:
print("Incorrect username or password. Please try again.")
login()
while True:
print("Welcome to the login system.")
print("1 - Register")
print("2 - Login")
print("3 - Exit")
choice = input("Please enter your choice: ")
if choice == "1":
register()
elif choice == "2":
login()
elif choice == "3":
break
else:
print("Invalid choice. Please try again.")
```
这个程序使用了一个字典来存储用户数据。在register函数中,它接受用户输入的用户名和密码,并将其存储在字典中。在login函数中,它要求用户输入其用户名和密码,并检查它们是否匹配存储在字典中的数据。如果用户输入的用户名和密码与存储的数据匹配,则登录成功,否则它会递归地再次请求用户登录。主循环提供了三个选项:注册、登录和退出系统。