python用户登录模块(不使用函数等方法)

Posted aliter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python用户登录模块(不使用函数等方法)相关的知识,希望对你有一定的参考价值。

*  用户登录模块

给定用户信息表,需要满足条件如下:

1. 输入用户名密码判断

2. 输入错误次数3次时,询问用户是否需要继续尝试,Y继续,N结束

3. 可支持多用户登录

技术分享图片
 1 # 方案一:输入用户名后立即判断一次,共三次
 2 li = [{username: qqq, password: www},
 3       {username: aaa, password: sss},
 4       {username: zzz, password: xxx}]
 5 
 6 # 将原列表转换为方便比较的新字典
 7 new_users_info = {}
 8 for i in li:
 9     new_users_info[i[username]] = i[password]
10 
11 count_try_username, count_try_password = 0, 0  # 用户名尝试次数,密码尝试次数
12 flag_username, flag_password, flag_selection = 1, 1, 1  # 循环到用户名输入,循环到密码输入,循环到是否继续
13 while flag_username:
14     count_try_username += 1
15     username_input = input(请输入用户名:)
16     if username_input in new_users_info.keys():  # 判断用户名是否存在
17         count_try_password = 0  # 用户名尝试次数归零/或归1
18         flag_password, flag_selection = 1, 1  # 所有循环重新开始
19         while flag_password:
20             password_input = input(请输入密码:)
21             if password_input == new_users_info[username_input]:  # 判断密码是否与用户名匹配
22                 print(登录成功!跳转到APP使用页面)
23                 flag_username = 0
24                 break
25             else:
26                 count_try_password += 1
27                 print(密码输入错误...%s次 % count_try_password)
28                 if count_try_password == 3:
29                     while flag_selection:
30                         user_selection = input(是否继续?Y/N)
31                         user_selection = user_selection.lower()
32                         if user_selection == y:
33                             count_try_username, count_try_password = 0, 0
34                             flag_password = 0
35                             flag_selection = 0
36                         elif user_selection == n:
37                             flag_username = 0
38                             flag_password, flag_selection = 0, 0
39                             print(ByeBye...)
40                         else:
41                             print(错误选择)
42     else:
43         print(用户名输入错误...%s次 % count_try_username)
44         if count_try_username == 3:
45             flag_password, flag_selection = 1, 1  # 所有循环重新开始
46             while flag_selection:
47                 user_selection = input(是否继续?Y/N)
48                 user_selection = user_selection.lower()
49                 if user_selection == y:
50                     count_try_username = 0
51                     flag_password = 0
52                     flag_selection = 0
53                 elif user_selection == n:
54                     flag_username = 0
55                     flag_password, flag_selection = 0, 0
56                     print(ByeBye...)
57                 else:
58                     print(错误选择)
判断用户名是否正确

以上代码会判断用户名错误三次的情况.

以下代码不判断用户名错误,只判断匹配.

技术分享图片
 1 # 用户登录(三次机会重试),可以支持多用户登录,三次后可选择Y/N决定是否继续尝试
 2 # 方案二:不判断用户名错误次数,只判断密码是否错误
 3 li = [{username: li_alex, password: SB},
 4       {username: wu_sir, password: sb},
 5       {username: xu_jin, password: student}]
 6 
 7 new_user_info = {}
 8 for user_info in li:
 9     new_user_info[user_info[username]] = user_info[password]
10 print(new_user_info)
11 
12 while 1:
13     count = 0
14     username_entry = input(Please Input an Username: )
15     if username_entry in new_user_info.keys():
16         password_entry = input(Please Input the Password: )
17         while count < 2 and password_entry != new_user_info[username_entry]:
18             count += 1
19             print(Wrong Password Input, %d time(s) left... % (3 - count))
20             password_entry = input(Please Input the Password: )
21         else:
22             if password_entry == new_user_info[username_entry]:
23                 print(Congratulations! Login Succeed!)
24                 break
25             elif count > 1:
26                 user_selection = input(Invalid time Exceeds Limited, Do You Wanna Try Again? Y/N )
27                 lower_entry = user_selection.lower()
28                 if lower_entry == y:
29                     continue
30                 elif lower_entry == n:
31                     print(You Give up Login attempt. Good luck...(手动微笑))
32                     break
33                 else:
34                     print(Wrong Selection...Please Try Again...)
35                     continue
36             else:
37                 print("error")
判断用户名和密码匹配

均不使用函数等方法,只用while/for/if-else循环

以上是关于python用户登录模块(不使用函数等方法)的主要内容,如果未能解决你的问题,请参考以下文章

Python 模块

python 模块学习

python之模块

python编程:函数式编程实现登录和注册

python的hashlib模块

关于drupal 7 用户登录 注册 修改注册信息等的问题