首页 > 代码库 > Python week1-练习1登陆接口

Python week1-练习1登陆接口

文件下载:practise1.tar

练习一:

  1. 输入用户名密码
  2. 认证成功后现实欢迎信息
  3. 输错三次后锁定
#!/usr/bin/env python
#Author:Austin

name = input("Please input user name:")

deny_file = open("deny.txt","r")
line = deny_file.readline()[:-1]
while line:
    if line == name:
        print("Sorry, the user has locked!")
        exit()
    line = deny_file.readline()[:-1]
deny_file.close()

password = input("Password:")

f = open("passwd.txt","r")
line = f.readline()
while line:
    _user_name = line.split(":")[0]
    _user_password = line.split(":")[1][:-1]

    if _user_name == name:
        if  _user_password == password:
            print("Welcome my friend",name)
            break
        else:
            count = 1
            while count < 3:
                password = input("Password wrong,input angain.")
                if _user_password == password:
                    print("Welcome my friend ",name)
                    break
                else:
                    count += 1
            if count == 3:
                print("You have tried three times, user {name} will be locked".format(name = name))
                deny_file = open("deny.txt","a")
                deny_file.write(name+"\n")
                deny_file.close()
    line = f.readline()
f.close()

后记:

1.第一周学习内容没有打开文件操作,自己搜索一下进行了操作。

2.写完以后觉得流程图确实非常重要,能缩短时间。对于山炮程序员的自己来说,画流程图觉得好麻烦。

3.写的时候觉得应该用子函数调用会更容易。但那时候写的差不多了,就不想改动了。

4.山炮程序员保证下次写程序多一点注释。

Python week1-练习1登陆接口