第一章 循环语句

控制语句 if...else

if 条件:

代码

else:

print(‘ ’)

if ...else...

if 条件:

代码

elif 条件:

代码

elif 条件:

代码

else:

代码

While 循环

count = 0

while count<=100:

print("loop",count)

count+=1

死循环

count=0

while True:

print('aisfioasudfiaus',count)

count+=1

循环终止语句

break

count=0

while count<100:

print('loop', count)

if count ==5:

break

count+=1

print('-----out of while loop ------')

continue

count=0

while count <=100:

count+=1

if count>5 and count <95:

continue

print("loop", count)

print("----out of while loop ----")

while ... else ...

count=0

while <=5:

count+=1

print("loop",count)

else:

print('')

print("-----------")

作业:

猜年龄

age_of_oldboy=8

age=input('请输入年龄>>: ')

if age==age_of_oldboy:

print('正确')

break

else:

print('错误,请重新输入)

age=input('请输入年龄>>: ')

if age==age_of_oldboy:

print('正确')

break

else:

print('错误,请重新输入)

age=input('请输入年龄>>: ')

if age==age_of_oldboy:

print('正确')

break

else:

print('错误超过三次,退出)

count=0<br />age_of_oldboy=8<br /><br />while count<3:<br />    age = int(input('请输入年龄>>: '))<br />    if age == age_of_oldboy:<br />        print('正确')<br />        break<br />    else:<br />        print('错误')<br />    count += 1<br /><br />    if count==3:<br />        agent=input('错误次数超过三次,继续按y,否则按n')<br />        if agent== 'y'or agent == 'Y':<br />            count=0<br />            continue<br />        elif agent == 'N' or agent == 'n':<br />            break<br /><br /><br /><br />登陆接口<br />用户3次认证失败后,退出程序,再次启动程序尝试登陆时,还是锁定状态(用户锁定状态存在文件里)
dic={'user1':{'psw':'123','count':0},<br />     'user2':{'psw':'123','count':0},<br />     'user3':{'psw':'123','count':0},<br />     }<br />while True:<br />    name=input('请输入用户名>: ')<br />    if  not name in dic:<br />        print('请重试')<br />        continue<br />    with open('user.txt','r') as read_f:<br />        bb=read_f.read().splitlines()<br />        if name in bb:<br />            print('账户已锁定')<br />            continue<br /><br /><br />    psw=input('请输入密码>: ')<br />    if dic[name]['count'] >2:<br />        print('锁定')<br />        with open('user.txt', 'a+') as write_f:<br />            write_f.write(name)<br />            write_f.write('\n')<br />        break<br /><br /><br />    if psw ==dic[name]['psw']:<br />        print('登陆成功')<br />        break<br />    else:<br />        print('请重试')<br />        dic[name]['count']+=1<br />        continue
<br /><br />