32ijhgf 发表于 2016-8-25 09:37:13

python中for循环是可以带else的



python中for循环是可以带else的,不满足for循环的条件,可以执行ELSE的内容!


#!user/bin/env python
#-*-coding:utf-8 -*-
# Author: Sy106


#read userinfo
account='user.txt'
f=file(account)
accountName=f.readlines()
print 'the Auth Names are:',accountName

for i in range(5):
    Login=False
    #read lock name into local file
    username=raw_input('please input your login name:').strip()
    lock='lock.txt'
    f=file(lock)
    lockName_list=[]
    for lockName in f.readlines():
      lockName=lockName.strip('\n')
      lockName_list.append(lockName)
    f.close()
    print 'lockName_list are:',lockName_list
    if username in lockName_list:
      print'The user %s has been lock!please use another username!'%username
      break
    for line in accountName:
      line=line.split()
      if username==line:#if the name is right ,input the passwd
            for j in range(3):
                passwd = raw_input('please input your password:').strip()
                if passwd==line:
                  print "Welcome %s login the system!"%username#login success
                  Login = True
                  break
            else:
                f=file(lock,'a')
                f.write('%s\n'%username)
                f.close()
                print 'Enter 3 times wrong password,the name %s is locked'%username
            if Login is True:break#jump out of for loop
    else:
      print 'please input the right name ,retry!'
    ifLogin is True:
      break#jump out of top for loop




woshipreson 发表于 2016-8-25 17:46:31

while/if/for 都带else语句
页: [1]
查看完整版本: python中for循环是可以带else的