python学习-python入门
开始学习python,开始记录。
第一个小程序:登陆系统
功能:1、通过文件名和密码导入用户名和密码~
2、用户输入用户名和密码
3、将用户输入的用户名进行比对,先判断用户名是否在黑名单里面,如果在黑名单里面就直接退出;如果不在黑名单里面就继续输入密码,然后将用户名和密码与正确的用户名密码匹配,匹配通过则显示登陆成功,匹配失败则提示重新输入密码,密码输入错误三次退出系统,锁定用户名,加入黑名单。
用到的知识点:1、文件读写操作。纪要:打开文件,只读用r,打开读且写入内容,用w+或者r+,w+是追加写,但是每次重新打开都会覆盖原来文件的内容。r+是直接在原来的文件 内容后面追加。
2、列表基本知识,字典基本知识
3、导入模块,是输入的密码不可见
源码:
#Author:qcg
import getpass info = open('/home/me/python-study/20170913/info.txt','r') #导入用户名密码 Blanklist=open('/home/me/python-study/20170913/blanklist.csv','r+') #username = input("please input username:") #passwd = input("please input your passqord:") #创建用户名密码字典 for line in info: user_list=line.split(',') i=0 dic_usr={} while i<len(user_list)/2 -1: dic_usr[user_list[(2*i)]]=user_list[(2*i+1)] i+=1 #print (dic_usr) username = input("please input username:") for line in Blanklist: # print (line) if username in line: print("sorry,you are forbidden to logn in") break count = 0 while count<3: #passwd = input("please input your password:")
passwd = getpass.getpass("please input your password:") if username in dic_usr and dic_usr[username]==passwd: #判断用户名是否在黑名单中,若不在则匹配用户名密码~ print("welcome to logn in") break else: print("please try again~~~") count+=1 else: print ("you have try too manay times,byebye") Blanklist.write(username) Blanklist.write(',') Blanklist.close() info.close()
运行结果:
[root@lmdx6688 20170914]# python3.5 lognin5.py
please input username:123
please input your password:
please try again~~~
please input your password:
please try again~~~
please input your password:
please try again~~~
you have try too manay times,byebye
[root@lmdx6688 20170914]# python3.5 lognin5.py
please input username:qcg
please input your password:
welcome to logn in