while True:
name = raw_input('what is your name?').strip()
if len(name) == 0:
continue
break
age = int(raw_input('what is your age?'))
sex = raw_input('what is your sex?').strip()
job = raw_input('what is your job?')
print """Personal Info:
Name:%s
Age :%d
Sex :%s
Job :%s
""" % (name,age,sex,job)
练习程序:
编写登陆接口
1.输入用户名密码
2.认证成功后显示欢迎信息
3.密码输错三次错误锁定
基础:
os.system 调取shell命令
执行返回结果为0为正确,256为错误
f=file('文件名','w') w写一个新文件,a为追加文件
f.write('文件的内容') 向文件内写内容
f.close() 关闭文件
a=file('文件名') 读取文件
a.read()
for line in a.read():print line 二进制方式读
for line in a.readlines():print line, 字符串方式读
定义列表
name = ['a','b','c']
name[0]为a
name[1]为b
line.split() 转换为列表
for line in a.readlines():print line.split()[1],
Start
#!/usr/bin/env python
import os
account_file = 'account.txt'
lock_file = 'lock.txt'
# put account in a list
f = file(account_file)
account_list = f.readlines()
f.close()
while True:
#put locked user info a lock list
f = file(lock_file)
lock_list = []
for i in f.readlines():
line = i.strip('\n')
lock_list.append(line)
f.close()
loginSuccess = False
username = raw_input('login:').strip()
if username in lock_list:
print "Sorry, you are already in the block list , get the fucking out !"
break
for line in account_list:
line = line.split()
if line[0] == username: #correct username
for i in range(3):
password = raw_input('passwd:').strip()
if password == line[1]: #correct passwd
print "Welcome %s login my system!" %username
loginSuccess = True
break
else:
f = file(lock_file,'a')
f.write('\n%s'%username)
f.close()
print "The password input error 3 times,going to lock %s" % username
if loginSuccess == True:break #jump out of the for loop
if loginSuccess == True:break #jump out of while loop
阅读(2169) | 评论(0) | 转发(1) |