函数登录注册事例

 1 #!/usr/bin/env python3
 2 # -*-conding:utf-8-**
 3 # __Author__:'liudong'
 4 
 5 
 6 def login(username,password):
 7     """
 8     用于用户登录
 9     :param username:
10     :param password:
11     :return:true,登录成功,否则登录失败
12     """
13     f = open('dblogin','r')
14     for line in f:  # 一行一行读取文件内容
15         line_list=line.split('|')
16         if line_list[0] == username and line_list[1] == password:
17             return True  # 找到匹配的用户返回退出
18     return False     # 如果循环读取后找不到匹配的用用户,返回False
19 
20 
21 def register(username, password):
22     """
23     用于用户注册
24     :param username:
25     :param password:
26     :return: None
27     """
28     f = open('dblogin', 'a')
29     temp = '\n'+username + '|' + password
30     f.write(temp)
31     f.close()
32 if __name__ == '__main__':
33     t = input('1:登录 2:注册')
34     if t == '1':
35         user = input('请输入用户名:')
36         pwd = input('请输入密码:')
37         r = login(user,pwd)
38         if r:
39             print('登录成功')
40         else:
41             print('登录失败')
42     elif t == '2':
43         user = input('请输入用户名:')
44         pwd = input('请输入密码:')
45         register(user, pwd)
View Code

 

posted @ 2016-11-01 22:48  ld1977  阅读(155)  评论(0编辑  收藏  举报