用Python写一个带有登录页面的好友管理系统代码
时间: 2024-03-22 19:40:09 浏览: 35
以下是一个简单的好友管理系统代码,包含登录页面的实现:
```python
import getpass
# 定义用户数据
users = {
'user1': 'password1',
'user2': 'password2',
'user3': 'password3'
}
# 定义好友数据
friends = {
'user1': ['friend1', 'friend2', 'friend3'],
'user2': ['friend4', 'friend5', 'friend6'],
'user3': ['friend7', 'friend8', 'friend9']
}
# 登录函数
def login():
username = input('请输入用户名:')
password = getpass.getpass('请输入密码:')
if username in users and password == users[username]:
print('登录成功!')
return username
else:
print('用户名或密码错误!')
return None
# 添加好友函数
def add_friend(username):
friend_name = input('请输入好友名字:')
if username in friends:
friends[username].append(friend_name)
else:
friends[username] = [friend_name]
print('添加好友成功!')
# 删除好友函数
def delete_friend(username):
friend_name = input('请输入要删除的好友名字:')
if username in friends and friend_name in friends[username]:
friends[username].remove(friend_name)
print('删除好友成功!')
else:
print('好友不存在!')
# 显示好友函数
def show_friends(username):
if username in friends:
print('您的好友列表如下:')
for friend in friends[username]:
print(friend)
else:
print('您还没有好友!')
# 主程序
def main():
username = None
while not username:
username = login()
while True:
print('请选择您要进行的操作:')
print('1. 添加好友')
print('2. 删除好友')
print('3. 显示好友列表')
print('4. 退出')
choice = input()
if choice == '1':
add_friend(username)
elif choice == '2':
delete_friend(username)
elif choice == '3':
show_friends(username)
elif choice == '4':
print('再见!')
break
else:
print('输入有误,请重新输入!')
if __name__ == '__main__':
main()
```
在这个代码中,我们使用了 `getpass` 模块来隐藏密码的输入,保证了登录信息的安全性。同时,我们使用了字典来存储用户和好友数据,实现了添加、删除和显示好友的功能。在主程序中,我们使用一个 `while` 循环来不断询问用户要进行的操作,直到用户选择退出为止。