首页 > 代码库 > Python作业 1

Python作业 1

用户有三次机会登录,登录成功,显示欢迎界面。

三次登录失败,账户被锁定,下次登录失败。

思路:

1:流程控制:for或者while循环,达到三次检测目的。

2:文件操作。

检测文档,一旦发现输入账户名在锁定文档中,显示锁定,登录失败。

第一次未经优化版本代码:

#_*_coding:utf-8_*_

times = 0 # 初始次数为0

Username = "wuqiangroy"
userpassword = "123321"
f = open("aaa.txt", "r")  # 以read模式打开aaa.txt文件,如果没有自动创建。
file_list = f.readlines()
f.close()

lock = [] #lock为一个列表。

for i in file_list:
    line = i.strip("\n")
    lock.append(line)

else:
    while times < 3:
        username = raw_input("Please enter the username: ")
        if username in lock: #检测输入帐户名是否在lock中,如果在,显示账户被锁定,然后break跳出循环。
            print "You account has been locked!"
            break
        password = raw_input("Please enter your password: ")
        if username == Username and password == userpassword:
            print "Successfull! Welcome log in!"
            break
        else:
            print "Your username or password is wrong! Please enter again! "
        
        times += 1
        print "You just have only %s shots!" %(3-times)
 
    else:
        print "The only 3 times you used out, the account has been locked!"
        f = open("aaa.txt", "w+") #以写入模式打开aaa.txt文件
        f.write("\n")
        f.write(username) #三次机会输入错误,将输入的名字写进aaa.txt文件中。
        f.close()

  

 

Python作业 1