1.python语言实现用户登录程序

#!/usr/bin/env python
# -*- coding:utf-8 -*-
count = 0
flag = 0
#提示用户需要做什么操作
option = int(input("提示:数字1为注册用户,数字2为登录系统,请选择:"))

if option == 1:
user = input("请输入用户名:")
passwd = input("请输入密码:")
f = open("user.txt","a")
f.write("%s:%s\n" % (user,passwd))
f.close()
elif option == 2:
while count < 3:
user = input("请输入登录名:")
passwd = input("请输入密码:")
UserPassList = [i.strip() for i in open('user.txt','r').readlines()] #按行读取文件内容,并去空格。
for i in UserPassList:
username = i.split(':')[0]
paasword = i.split(':')[1]
if user == username and passwd == paasword:
flag = 1
break
if flag == 1:
break
else:
count += 1
if flag == 1:
print("登录成功")
else:
print("登录失败")
else:
print("输入错误,SX")
posted @ 2016-10-27 23:28  小忍者  阅读(448)  评论(1编辑  收藏  举报