python入门:最基本的用户登录用户登录,三次错误机会

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #用户登录,三次错误机会
 4 """
 5 导入getpass,给x赋值为1,while真,循环开始,
 6 user的赋值等于用户输入的内容,pwd的赋值等于用户输入的内容并且不可见
 7 如果用户输入user的赋值等于"admin"而且用户输入pwd的赋值等于"admin888"
 8 打印"login successfully!(登陆成功!)"换行 打印"welcome!:(欢迎:)user",break跳出当前循环
 9 否则如果x的赋值等于3,打印"Failure three times!(失败三次!)"break跳出当前循环
10 否则打印"login failure!There are three more chances!(登录失败!有三次机会)"
11 x的赋值每循环一次加1,当循环3次后就满足了x赋值为3,break跳出当前循环!
12 """
13 import getpass
14 x = 1
15 while True:
16     user = raw_input("user:")
17     pwd = getpass.getpass('password:')
18     if user == "admin" and pwd == "admin888":
19         print("login successfully!" + "\n" +"welcome!:" + user)
20         break
21     elif x == 3:
22         print("Failure three times!")
23         break
24     else:
25         print("login failure!There are three more chances!")
26     x += 1

 

posted @ 2017-11-23 18:49  俊豪郎  阅读(782)  评论(0编辑  收藏  举报